public static void main(String[] args){ File inFile = new File("d://1.txt"); // 实例 输入的 File 类 File outFile = new File("d://2.txt"); // 实例 输出的 File 类 BufferedInputStream bis = null; // 实例 输入的缓冲 流类 BufferedOutputStream bos = null; // 实例 输出的缓冲 流类 try { byte[] buffered = new byte[1024]; // 读文件的 字节缓冲区1 个字节 // 通过 输入的 File 类 来实例 文件输入流 ,通过实例好的文件输入流来实例 缓冲流 bis = new BufferedInputStream(new FileInputStream(inFile)); // 通过 输出的 File 类 来实例 文件输出流 ,通过实例好的文件输入流来实例 缓冲流 bos = new BufferedOutputStream(new FileOutputStream(outFile)); while(bis.read(buffered) != -1){ // 一次一次的往缓冲区 里读(1字节) bos.write(buffered); //写 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { bos.flush(); // 把还残留在内存里的字节流 flush 进文件里 bos.close(); // 关闭 bis.close(); // 关闭 } catch (IOException e) { e.printStackTrace(); } } /* * 如果用字节缓冲流的话,会先写进内存,直到读写完,才写入硬盘,缓冲不伤硬盘 * 如果不用缓冲的话,就读一个字节往硬盘里写一次. */ }
java程序中输入流和输出流的处理过程
最新推荐文章于 2020-12-01 13:47:24 发布