一、代码实现原理
二、三种代码实现
通过currentTimeMillis来测试复制时间;
1、使用字节流
public static void main(String[] args) throws IOException {
long s=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("D:\\Users\\Desktop\\新建文件夹\\a.txt");
FileOutputStream fos=new FileOutputStream("D:\\Users\\Desktop\\a.txt");
int length=0;
byte[] bytes = new byte[1024];
while((length=fis.read(bytes))!=-1){
fos.write(length);
}
fos.close();
fis.close();
long e=System.currentTimeMillis();
System.out.println(e-s);
}
2、使用字符流
public static void main(String[] args) throws IOException {
long s=System.currentTimeMillis();
FileWriter fw=new FileWriter("D:\\Users\\Desktop\\新建文件夹\\a.txt");
FileReader fr=new FileReader("D:\\Users\\Desktop\\新建文件夹\\a.txt");
int len=0;
char[] chars = new char[1024];
while((len=fr.read(chars))!=-1){
fw.write(len);
}
fos.close();
fis.close();
long e=System.currentTimeMillis();
System.out.println(e-s);
}
3、使用缓冲流
public static void main(String[] args) throws IOException {
long s=System.currentTimeMillis();
BufferedReader br=new BufferedReader(new FileReader("D:\\Users\\Desktop\\新建文件夹\\a.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\Users\\Desktop\\新建文件夹\\a.txt"));
String line;
while((line=br.readLine())!=null){
bw.write(line);
}
bw.close();
br.close();
long e=System.currentTimeMillis();
System.out.println(e-s);
四、效率(速度)
缓冲流>字符流>字节流