主要发生在文件处理中,比如使用I/O流读写文件时。程序会影响到磁盘使用率。
public class TestDisk {
public static void main(String[] args) throws IOException {
copyFileByBuf("E:\\123.zip","D:");
}
public static void copyFileByBuf(String srcPath,String destDirPath) throws IOException {
File srcFile = new File(srcPath);
File destDirFile = new File(destDirPath);
if(!destDirFile.exists()){
destDirFile.mkdirs();
}
File descFile = new File(destDirPath+File.separator+srcFile.getName());
FileInputStream inputStream = new FileInputStream(srcFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream(descFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] buf = new byte[1024*16];
int length = bufferedInputStream.read(buf);
while(length != -1){
bufferedOutputStream.write(buf,0,length);
length = bufferedInputStream.read(buf);
}
bufferedInputStream.close();
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}