IO流单字节文件复制粘贴和批量字节文件复制粘贴。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class FuzhiZhantie {
public static void main(String[] args) {
File();
File1();
}
//单字节复制
public static void File() {
try {
FileInputStream in = new FileInputStream("C:\\123.jpg");
FileOutputStream out = new FileOutputStream("E:/12345.jpg");
long t1=System.currentTimeMillis();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
long t2=System.currentTimeMillis();
long t=t2-t1;
System.out.println("复制完成,需要时间:"+t);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//批量字节复制
public static void File1() {
try {
FileInputStream in = new FileInputStream("C:\\123.jpg");
FileOutputStream out = new FileOutputStream("E:/123456789.jpg");
long t1=System.currentTimeMillis();
byte [] buf=new byte[1024];
int length;
while((length=in.read(buf))!=-1) {
out.write(buf,0,length);
}
long t2=System.currentTimeMillis();
long t=t2-t1;
System.out.println("复制完成,需要时间:"+t);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}