package xxx;
import java.io.*;
public class JavaApplication1 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method1("E:/eclipse-workspace/java2/data/a.txt", "E:/eclipse-workspace/java2/data/b.txt");
method2("E:/eclipse-workspace/java2/data/a.txt", "E:/eclipse-workspace/java2/data/b.txt");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
}
输出结果:
(1)基本字节流一次读写一个字节
(2)基本字节流一次读写一个字节数组
第一种方法读写速度优于第二种方法