黑马程序员技术交流社区
标题:
IO九种复制方法
[打印本页]
作者:
王贺明
时间:
2015-10-8 00:40
标题:
IO九种复制方法
package cn.itcast.day21;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 九种复制方法
*/
public class Demo_nine_Copy {
public static void main(String[] args) throws IOException {
long currentTimeMillis = System.currentTimeMillis();
method1();
long currentTimeMillis2 = System.currentTimeMillis();
method2();
long currentTimeMillis3 = System.currentTimeMillis();
method3();
long currentTimeMillis4 = System.currentTimeMillis();
method4();
long currentTimeMillis5 = System.currentTimeMillis();
method5();
long currentTimeMillis6 = System.currentTimeMillis();
method6();
long currentTimeMillis7 = System.currentTimeMillis();
method7();
long currentTimeMillis8 = System.currentTimeMillis();
method8();
long currentTimeMillis9 = System.currentTimeMillis();
method9();
long currentTimeMillis10 = System.currentTimeMillis();
System.out.println("普通字节流 一次一个字节: "+(currentTimeMillis2-currentTimeMillis));
System.out.println("普通字节流 一次一个字节数组: "+(currentTimeMillis3-currentTimeMillis2));
System.out.println("高效字节流 一次一个字节: "+(currentTimeMillis4-currentTimeMillis3));
System.out.println("高效字节流 一次一个字节数组: "+(currentTimeMillis5-currentTimeMillis4));
System.out.println("普通字符流 一次一个字符: "+(currentTimeMillis6-currentTimeMillis5));
System.out.println("普通字符流 一次一个字符数组: "+(currentTimeMillis7-currentTimeMillis6));
System.out.println("高效字节流 一次一个字符数组: "+(currentTimeMillis8-currentTimeMillis7));
System.out.println("高效字节流 一次一个字符: "+(currentTimeMillis9-currentTimeMillis8));
System.out.println("高效字节流 一次一行: "+(currentTimeMillis10-currentTimeMillis9));
}
//普通字节流 一次一个字节
public static void method1() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
//普通字节流 一次一个字节数组
public static void method2() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
int len=0;
byte[] bytes = new byte[1024];
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
//高效字节流 一次一个字节
public static void method3() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b=0;
while((b=bis.read())!=-1){
bos.write(b);
}
bos.close();
bis.close();
}
//高效字节流 一次一个字节数组
public static void method4() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len=0;
byte[] bytes = new byte[1024];
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
//普通字符流 一次一个字符
public static void method5() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
//普通字符流 一次一个字符数组
public static void method6() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
int len = 0;
char[] bytes = new char[1024];
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
//高效字符流 一次一个字符数组
public static void method7() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
int len = 0;
char[] bytes = new char[1024];
while((len=br.read(bytes))!=-1){
bw.write(bytes,0,len);
}
bw.close();
br.close();
}
//高效字符流 一次一个字符
public static void method8() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
int b = 0;
while((b=br.read())!=-1){
bw.write(b);
}
bw.close();
br.close();
}
//高效字符流 一次一行
public static void method9() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
String str;
while((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
}
bw.close();
br.close();
}
}
复制代码
作者:
ksh
时间:
2015-10-8 07:08
学习了,,,条理清楚,,
作者:
fwqk123
时间:
2015-10-8 10:32
每种方法具体用途是什么呢 困惑啊
作者:
itheima_casper
时间:
2015-10-8 13:31
搞个大文件试试效果
作者:
itheima_casper
时间:
2015-10-8 13:47
30M文件的效果:
普通字节流 一次一个字节: 271968
普通字节流 一次一个字节数组: 342
高效字节流 一次一个字节: 1554
高效字节流 一次一个字节数组: 113
普通字符流 一次一个字符: 2699
普通字符流 一次一个字符数组: 409
高效字符流 一次一个字符数组: 435
高效字符流 一次一个字符: 1835
高效字符流 一次一行: 537
结果是:字节+数组+buffered 最高效
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2