/*
*大文件复制时,有可能遇到内存不足的情况,比如,要复制一个10G的文件,你的内存只有4G,那么这时候就要选择分批复制。代码如下:
*/
package sxt1102;
import java.io.*;
public class Test {
/**
*
* 大文件处理时,要分批处理;
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("c:/IO 文件复制1.mp4");
File file2 = new File("c:/IO 文件复制3.mp4");
if (file.exists()) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
byte[] b = new byte[1024];//0-1024
int count = fileInputStream.read(b, 0, 1024);
while (count > 0) {
fileOutputStream.write(b, 0, count);
count = fileInputStream.read(b, 0, 1024);
//0-1023
//1024-20xxx
//
System.out.println(count);
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("文件复制成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
*count = fileInputStream.read(b, 0, 1024);
*java中,每次读完的时候,会自动指向下一个,也就是第二个下标0,变成了下标1024。
*/