//完成复制文件的9种方法
public class Demo1 {
public static void main(String[] args) throws IOException {
//比较一下以下几种复制文件的方法的耗时
long before = System.currentTimeMillis();
// method1();
// method2();
// method3();
// method4();
// method5();
// method6();
// method7();
// method8();
method9();
long after = System.currentTimeMillis();
System.out.println(after - before);
}
//基本字节流一次复制一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
//基本字节流一次复制一个字节数组
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");
int len;
byte[] bytes = new byte[1024];
while ((len = fis.read()) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
}
//高效字节流一次复制一个字节
private static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("b.txt"));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
//高效字节流一次复制一个字节数组
private static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("b.txt"));
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
}
//基本字符流一次复制一个字节
private static void method5() throws IOException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
int b;
while ((b = fr.read()) != -1) {
fw.write(b);
}
fr.close();
fw.close();
}
//基本字符流一次复制一个字节数组
private static void method6() throws IOException {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
int len;
char[] chars = new char[1024];
while ((len = fr.read(chars)) != -1) {
fw.write(chars, 0, len);
}
fr.close();
fw.close();
}
//高效字符流一次复制一个字节
private static void method7() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
int b;
while ((b = br.read()) != -1) {
bw.write(b);
}
br.close();
bw.close();
}
//高效字符流一次复制一个字符数组
private static void method8() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
int len;
char[] chars = new char[1024];
while ((len = br.read(chars)) != -1) {
bw.write(chars, 0, len);
}
br.close();
bw.close();
}
//高效字符流一次复制一行
private static void method9() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
}
|
|