JAVA Io 缓冲输入输出流
java中提供带缓冲的输入输出流。在打开文件进行写入或读取操作时,都会加上缓冲,提高了IO读写性能。
1. BufferedInputStream 缓冲输入流
2. BufferedOutputStream 缓冲输出流
使用带缓冲的输入输出流写一个拷贝文件的操作。
package com.dcz.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class BufferStream { /** * 拷贝文件带缓冲 * * @param srcFile * @param destFile * @throws Exception */ public static void copyFileByBuffer(File srcFile, File destFile) throws Exception { // 缓冲输入流 BufferedInputStream bufferInputStream = new BufferedInputStream( new FileInputStream(srcFile)); // 缓冲输出流 BufferedOutputStream bufferOutputStream = new BufferedOutputStream( new FileOutputStream(destFile)); int bytes = 0; while ((bytes = bufferInputStream.read()) != -1) { bufferOutputStream.write(bytes); bufferOutputStream.flush(); } bufferOutputStream.close(); bufferInputStream.close(); } public static void main(String[] args) throws Exception { BufferStream.copyFileByBuffer(new File("D:\\cglib-3.1.jar"), new File( "D:\\aa.jar")); } }