java-IO-BIO-工具类
来源:互联网 发布:深泉学院知乎 编辑:程序博客网 时间:2024/05/22 19:48
汇总BIO各种流操作的工具类
package io;import javax.crypto.*;import java.io.*;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.text.SimpleDateFormat;import java.util.*;/** * 汇总BIO各种流操作的工具类 */public class BIOUtil { /** * 不建议单线程用 导致死锁 read方法阻塞式方法 read()方法 没有读到数据的 线程处于等待状态 一录入 读取线程唤醒 * <p> * 两个管道 一个线程读 一个线程写 两个管道接起来 * <p> * connect方法创建空的 * * @throws IOException */ public void pipedInputStream() throws IOException { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(); in.connect(out);// Reader r = new Reader(in);// Write w = new Write(out);// new Thread(r).start();// new Thread(w).start(); }/////////////////////////////////////////////////////////////////////////////////////////// public void randomReadFile(String inputFilePath) throws IOException { // System.out.println(Integer.toBinaryString(258)); RandomAccessFile raf = new RandomAccessFile(inputFilePath, "r"); // 调整对象中的指针 前提是数据是有规律的 // seek 前后都有值 爱指哪儿 指哪儿 // raf.seek(8*0);第一个人//从第8位字节后面开始前取 // raf.seek(8*1);//第二个人 从第8位字节后面开始前取 // 跳过指定的字节数 raf.skipBytes(8);// 可以往下跳 不能往回跳 byte[] buf = new byte[4]; raf.read(buf); String name = new String(buf); int age = raf.readInt(); System.out.println("name=" + name); System.out.println("age=" + age); // raf.write("haha".getBytes()); raf.close(); } // 给数组元素设置 获取数组数据 // 该类不算是IO体系中子类 而是直接继承Object // 但是它是IO包中成员 因为它具备读和写功能 // 内部封装了一个数组 而且通过指针对数组的元素进行操作 // 可以通过getFilePointer获取指针位置 // 同时可以通过seek改变指针的位置 // 有一些读写方法 // 其实完成读写的原理就是内部封装了字节输入流和输出流 // 读取流和写入流 全封装了 /** * 局限性;通过构造函数可以看出 该类只能操作文件 硬盘 而且操作文件还有模式:只读r 读写rw等 * <p> * 如果模式为R 只读 不会创建文件 回去读取一个已存在的文件 如果该文件不存在 则会出现异常 * 如果模式为rw 操作的文件不存在 则会自动创建 如果存在则不会覆盖 * <p> * 应用场景: 实现数据的分段写入:一个线程负责一段 下载软件原理 多线程下载 一般的流从头往后写 操作数据不连续 * <p> * 而且该对象的构造函数要操作的文件不存在 会自动创建 如果存在 则会覆盖 * * @throws IOException */ public static void randomWriteFile(String outputFilePath) throws IOException { // System.out.println(Integer.toBinaryString(258)); RandomAccessFile raf = new RandomAccessFile(outputFilePath, "rw"); raf.write("李四".getBytes()); //// write(int val) 输出数据的最低八位 // writeInt(int val) 把数据的32位都输出 可以操作基本数据类型 // raf.write(258); raf.writeInt(97); raf.write("王五".getBytes()); raf.writeInt(99); raf.close(); } public static void randomWriteFile_2(String outputFilePath) throws IOException { RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw"); raf.seek(8 * 0);// 还能对数据进行修改 不覆盖文件 输出流 一new对象 就覆盖 raf.write("周期".getBytes()); raf.writeInt(99);//ASCII码 // 随机往里读写 }///////////////////////////////////////////////////////////////////////////////// public static void getFileInfo(String intputFilePath) throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream( intputFilePath)); int num = dis.readInt(); boolean b = dis.readBoolean(); double d = dis.readDouble(); System.out.println("num=" + num); System.out.println("b=" + b); System.out.println("d=" + d); dis.close(); } public static void writeBinData() throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream( "data.txt")); dos.writeInt(234);// 四个字节 dos.writeBoolean(true);// 一个字节 dos.writeDouble(9887.543);// 八个字节 dos.close(); } public static void writeByUTF8(String outPutFilePath) throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream( outPutFilePath)); // 被修改的编码 只能用它来读 dos.writeUTF("你好"); dos.close(); } public static void readUTF8(String inputFilePath) throws IOException { // DataInputStream dis =new DataInputStream(new // FileInputStream(inputFilePath)); DataInputStream dis = new DataInputStream( new FileInputStream(inputFilePath)); // 从流 in 中读取用 UTF-8 修改版格式编码的 Unicode 字符格式的字符串;然后以 String 形式返回此字符串。 // EOFException 如果此输入流在读取所有字节之前到达末尾 文件的字节数少于要读的字节数 String s = dis.readUTF(); System.out.println(s); dis.close(); } // // CharArrayReader CharArrayWriter StringReader StringWriter public static void writeByCode(String content,String code,String outputFilePath) throws IOException { // OutputStreamWriter osw = new OutputStreamWriter(new // FileOutputStream("utf.txt"),"utf-8"); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream( outputFilePath), code);// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(// "gbk.txt"), "gbk"); osw.write(content); osw.close(); } public void readByCode(String code,String outputFilePath) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream( outputFilePath), code);// InputStreamReader isr = new InputStreamReader(new FileInputStream(// "gbk.txt"), "utf-8"); char[] buf = new char[10]; int len = isr.read(buf); String str = new String(buf, 0, len); System.out.println(str); isr.close(); } // ByteArrayInputStream 缓冲区没有 // 内部可变长度的数组 用于操作字节数组的流对象 // ByteArrayInputStream:在构造的时候 需要接收数据源 而且数据源是一个字节数组 // ByteArrayOutputStream:在构造的时候 不用定义数据目的 因为该对象中已经内部封装了可变长度的字节数组 // 因为这两个流对象都操作的数组 并没有使用系统资源 所以 不用close关闭 能继续被使用 /** * 在流操作规律讲解时 * 源设备 键盘 System.in 硬盘 FileStream 内存ArrayInputStream 目的设备 控制台System.out * 硬盘FileStream 内存ArrayOutputStream 用流的读写来操作数组 封装性 复用性 数组的遍历 即读 数组元素的设置 即为写 * @throws IOException * @throws FileNotFoundException */ public void writeByteArray(String outPutFilePath) throws FileNotFoundException, IOException { // 数据源 ByteArrayInputStream bis = new ByteArrayInputStream( "ABCDEFG".getBytes()); // 数据目的 ByteArrayOutputStream bos = new ByteArrayOutputStream(); int by = 0; while ((by = bis.read()) != -1) { bos.write(by); } System.out.println(bos.size()); System.out.println(bos.toString()); bos.writeTo(new FileOutputStream(outPutFilePath)); } private static void secretDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException { // 加密器/解密器 Cipher cipher = Cipher.getInstance("AES");// 对称加密算法:AES DES // 从硬盘上恢复key FileInputStream fisKey = new FileInputStream("zxx_secret.key"); ObjectInputStream oisKey = new ObjectInputStream(fisKey); Key key = (SecretKey) oisKey.readObject(); oisKey.close(); fisKey.close(); // 处理是加密还是解密 操作模式 1 加密 2 解密 3 其他 cipher.init(Cipher.DECRYPT_MODE, key); FileInputStream fisDat = new FileInputStream("zxx.dat"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // copyStream(fisDat, baos); // byte[] src = baos.toByteArray(); // byte[] result = cipher.doFinal(baos.toByteArray()); // fisDat.close(); // baos.close(); // System.out.println("--" + new String(result)); byte[] src = new byte[fisDat.available()];// 硬盘上的数据 同城是文件大小 如果是 // 网络上的数据可能是断的 int len = fisDat.read(src); int total = 0; while (total < src.length) { // 尽可能地读取剩下的 total += len; len = fisDat.read(src, total, src.length - total);// 如果读取的最大字节数是0 // 返回0 } byte[] result = cipher.doFinal(src); fisDat.close(); System.out.println("--" + new String(result)); }////////////////////////////////////////////////////////////////////////// /** * 序列化到文件 * * @throws FileNotFoundException * @throws IOException */ public static void writeObj() throws FileNotFoundException, IOException { // 存的时候保存成person.object ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( "obj.txt")); oos.writeObject(new Student("lisi", 39, "kr")); // oos.writeObject(new Student("lisi",39,"kr")); // oos.writeObject(new Student("lisi",39,"kr")); // oos.writeObject(new Student("lisi",39,"kr")); // oos.writeObject(new Student("lisi",39,"kr")); 自动打包对象 可以存多个对象 // 按顺序read一次访问一个对象 再read再访问一个对象 oos.close(); } /** * 反序列化 * * @throws FileNotFoundException * @throws IOException * @throws ClassNotFoundException */ public static void readObj() throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "obj.txt")); Student stu = (Student) ois.readObject();// 对象的本地持久化 System.out.println(stu); ois.close(); }//////////////////////////////////////////////////////////////////////////////////////// /** * 文件合并 * * @throws IOException */ public static void merge() throws IOException { ArrayList<FileInputStream> al = new ArrayList<FileInputStream>(); for (int x = 1; x <= 9; x++) { al.add(new FileInputStream("c:\\split_files\\" + x + ".part")); } final Iterator<FileInputStream> it = al.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { @Override public boolean hasMoreElements() { return it.hasNext(); } @Override public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("c:\\split_files\\0.avi"); byte[] buf = new byte[1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close(); } /** * 合并文件(流的逻辑串联) * * @throws IOException */ public static void sequence(String[] inputFiles, String mergedFilePath) throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); for (String inputFile : inputFiles ) { v.add(new FileInputStream(inputFile)); }// v.add(new FileInputStream("c:\\1.txt"));// v.add(new FileInputStream("c:\\2.txt"));// v.add(new FileInputStream("c:\\3.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream(mergedFilePath); byte[] buf = new byte[1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close();// 关闭所有流 } /** * 分割文件 * * @throws IOException */ public static void splitFile(String inputFilePath, String outPutPath) throws IOException { FileInputStream fis = new FileInputStream(inputFilePath); FileOutputStream fos = null; // 过度 内存溢出 挂了 /** * 当文件过大 数组装不下 内存会溢出 时 一个流对象操作100M 数据 重新创建输出流对象 往另外一个文件写入100M 装一次 * * 当装到等于100M 重新创建一个输出流对象 往另外一个文件输出100M */ byte[] buf = new byte[1024 * 1024]; int len = 0; int count = 1; while ((len = fis.read(buf)) != -1) { fos = new FileOutputStream("outPutPath\\" + (count++) + ".part");// 碎片文件 fos.write(buf, 0, len); fos.close(); } fis.close(); } /** * 输出系统信息到文件 */ public static void systemInfo2File() { Properties prop = System.getProperties(); try { // System.out.println(prop); // prop.list(System.out); // JVM每次启动时 要获取本地系统的信息 prop.list(new PrintStream("sysinfo.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 异常信息输出到文件 */ @SuppressWarnings("hiding") public static void exceptionInfo2File() { PrintStream ps = null; try { int[] arr = new int[2]; System.out.println(arr[3]); } catch (Exception e) { // e.printStackTrace(System.out);默认 try { // e.printStackTrace(new PrintStream("a.txt")); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd HH:mm:ss"); String s = sdf.format(d); ps = new PrintStream("exeception.log"); // ps.write(d.toString().getBytes()); ps.println(s); System.setOut(ps); } catch (FileNotFoundException e1) { throw new RuntimeException("日志文件创建失败"); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(System.out); } finally { if (ps != null) try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 将文件信息输出到控制台 * * @param inputFilePath */ public static void file2Console(String inputFilePath) { // 为了提高效率 将字符流进行缓冲区技术的高效操作 使用BufferedReader BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new BufferedReader(new InputStreamReader( new FileInputStream(inputFilePath))); // 键盘录入的最常见写法 bufw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while ((line = bufr.readLine()) != null) { if ("over".equals(line)) break; bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufr != null) { try { bufr.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 将键盘输入内容写入文件 * * @param outputFilePath */ public static void keyboard2File(String outputFilePath) { // 为了提高效率 将字符流进行缓冲区技术的高效操作 使用BufferedReader BufferedReader bufr = null; BufferedWriter bufw = null; try { // 获取键盘录入对象 // InputStream in = System.in; // 将字节流对象转成字符流对象 使用转换流 InputStreamReader // InputStreamReader isr = new InputStreamReader(in); // bufr = new BufferedReader(isr); bufr = new BufferedReader(new InputStreamReader(System.in)); // OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out);//内部有缓冲区 // 键盘录入的最常见写法 bufw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outputFilePath))); String line = null; while ((line = bufr.readLine()) != null) { if ("over".equals(line)) break; bufw.write(line); bufw.newLine(); bufw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufr != null) { try { bufr.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 将键盘输入内容输出到控制台 */ public static void keyboard2Console() { // 为了提高效率 将字符流进行缓冲区技术的高效操作 使用BufferedReader BufferedReader bufr = null; BufferedWriter bufw = null; try { // 获取键盘录入对象 // InputStream in = System.in; // 将字节流对象转成字符流对象 使用转换流 InputStreamReader // InputStreamReader isr = new InputStreamReader(in); // bufr = new BufferedReader(isr); bufr = new BufferedReader(new InputStreamReader(System.in)); // OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out);//内部有缓冲区 // 键盘录入的最常见写法 bufw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while ((line = bufr.readLine()) != null) { if ("over".equals(line)) break; bufw.write(line.toUpperCase()); bufw.newLine(); bufw.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufr != null) { try { bufr.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 写入文本内容到文件 * * @param content * @param inputFilePath * @param isAppend */ public static void writeText(String content, String inputFilePath, Boolean isAppend) { OutputStreamWriter fw = null; try { // 传递true参数 代表不覆盖已有的文件 并在已有文件的末尾处进行数据的续写 if (isAppend) { fw = new FileWriter(inputFilePath, true); } else { fw = new FileWriter(inputFilePath, false); } // windows:\r\n linux:\n// fw.write("nihao\r\nxiexie"); fw.write(content); } catch (IOException e) { e.printStackTrace(); System.out.println(e); } finally { try { if (fw != null) fw.close(); fw = null; } catch (IOException e) { e.printStackTrace(); } } } /** * 读取文本文件 * * @param inputFilePath * @return */ public String readText(String inputFilePath) { FileReader fr = null; StringBuilder sb = new StringBuilder(); try { fr = new FileReader(inputFilePath); char[] buf = new char[1024]; int num = 0; while ((num = fr.read(buf)) != -1) { sb.append(new String(buf, 0, num)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } ////////////////////////////////////////////////////////////////////////////////////////// /** * 读取二进制文件 * * @param inputFilePath * @return */ public String readBin(String inputFilePath) {// 虚拟机启动 默认64M的内存空间 FileInputStream fis = null; try { fis = new FileInputStream(inputFilePath); int num = fis.available();// 读取一定数量后 就不会在往availale里面装了 大体数据 byte[] buf = new byte[num];// 定一个刚刚好的缓冲区 不用再循环了 如果数据量太大了不适合 fis.read(buf); return new String(buf); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return ""; } /** * 拷贝二进制文件 * * @param orginFilPath * @param targetFilePath */ public static void copyBinFile(String orginFilPath, String targetFilePath) { BufferedInputStream bufis = null; BufferedOutputStream bufos = null; try { bufis = new BufferedInputStream(new FileInputStream(orginFilPath)); bufos = new BufferedOutputStream(new FileOutputStream(targetFilePath)); byte[] buf = new byte[1024]; int len = 0; while ((len = bufis.read(buf)) != -1) { bufos.write(buf, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("复制文件失败"); } finally { try { if (bufis != null) bufis.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("读取关闭失败"); } try { if (bufos != null) bufos.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("写入关闭失败"); } } } /** * 拷贝文本文件 * * @param orginFilePath * @param targetFilePath */ public static void copyText(String orginFilePath, String targetFilePath) { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(orginFilePath); fw = new FileWriter(targetFilePath); char[] buf = new char[1024]; int len = 0; while ((len = fr.read(buf)) != -1) { fw.write(buf, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("copyText fail!"); } finally { if (fr != null) try { fr.close(); } catch (IOException e) { e.printStackTrace(); } if (fw != null) try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 通过缓冲区拷贝文本文件 * * @param orginFilePath * @param targetFilePath */ public static void copyTextByBuf(String orginFilePath, String targetFilePath) { BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new BufferedReader(new FileReader(orginFilePath)); bufw = new BufferedWriter(new FileWriter(targetFilePath)); String line = null; while ((line = bufr.readLine()) != null) { bufw.write(line); bufw.newLine(); bufw.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("读写失败"); } finally { if (bufr != null) try { // 其实关闭缓冲区 就是在关闭缓冲区中的流对象 bufr.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("读取关闭失败"); } if (bufw != null) try { // 其实关闭缓冲区 就是在关闭缓冲区中的流对象 bufw.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("写入关闭失败"); } } }}
0 0
- java-IO-BIO-工具类
- Java—BIO (Block IO)
- Java IO: BIO, NIO, AIO
- 【java】IO详解之BIO
- 【基础-java-BIO流】BIO(1)常用IO流引索
- Java IO工具类
- java io bio nio aio 详解
- java io bio nio aio 详解
- Java的IO:BIO | NIO | AIO
- java io bio nio aio 详解
- JAVA-IO模型(BIO,NIO,AIO)
- java之阻塞IO(BIO)
- Java之IO,BIO,NIO,AIO
- Java IO知识:NIO,BIO,AIO
- Java IO:面向流、同步、阻塞式IO(BIO)
- java IO工具类大全
- Java IO流工具类
- 【Java IO模式】Java BIO NIO AIO总结
- BJ模拟(2) D2T1 简单粗暴的题目
- 微信公众号加入图灵机器人
- MyEclipse导入Eclipse中的Web项目,不能识别为Web项目
- 冒泡事件流的应用——事件委托
- 大型分布式网站架构技术总结
- java-IO-BIO-工具类
- Java中反射调用私有方法出现NoSuchMethodException
- C/C++学习之路之标准输入输出
- <Python核心编程(第二版)>第八章练习题
- 小明的调查作业
- 一篇文章学会spark-streaming
- 资源命名规范
- OnItemClickListener中OnItemClickListener参数详解
- 数据结构知识补全计划