字节流方法:
public class Lianxi5 {
publicstatic void main(String[] args) throws IOException {
使用普通流一次一个字节的方法复制文件
long t1 = System.currentTimeMillis();
FileInputStream f1 = new FileInputStream("C:\\Users\\Administrator\\Desktop\\day11\\video\\1.wmv");
FileOutputStream f2 = newFileOutputStream("C:\\Users\\Administrator\\Desktop\\11日录屏");
int n1;
while((n1 = f1.read()) != -1){
f2.write(n1);
}
f1.close();
f2.close();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);//379878 复制文件所用时长
使用一次一个字节数组的方法复制文件
long t3 = System.currentTimeMillis();
FileInputStream f3 = new FileInputStream("C:\\Users\\Administrator\\Desktop\\day11\\video\\1.wmv");
FileOutputStream f4 = newFileOutputStream("C:\\Users\\Administrator\\Desktop\\11日录屏2");
byte [] b3 = new byte[1024];
int len2;
while((len2 = f3.read(b3)) != -1){
f4.write(b3);
}
long t4 = System.currentTimeMillis();
System.out.println(t4-t3);//608 复制文件所用时长
使用高效流一次一个字节的方法复制文件
long t5 = System.currentTimeMillis();
FileInputStream f5 = newFileInputStream("C:\\Users\\Administrator\\Desktop\\day11\\video\\1.wmv");
FileOutputStream f6 = newFileOutputStream("C:\\Users\\Administrator\\Desktop\\11日录屏3");
BufferedInputStream f7 = new BufferedInputStream(f5);
BufferedOutputStream f8 = new BufferedOutputStream(f6);
int len3;
while((len3 = f7.read()) != -1){
f8.write(len3);
}
f7.close();
f8.flush();
f8.close();
long t6 = System.currentTimeMillis();
System.out.println(t6-t5);//复制文件所用时长 3604
使用高效流一次一个字节数组复制文件
long t7 = System.currentTimeMillis();
FileInputStream f9 = newFileInputStream("C:\\Users\\Administrator\\Desktop\\day11\\video\\1.wmv");
FileOutputStream f10 = newFileOutputStream("C:\\Users\\Administrator\\Desktop\\11日录屏3");
BufferedInputStream f11 = new BufferedInputStream(f9);
BufferedOutputStream f12 = new BufferedOutputStream(f10);
int len4;
byte [] b4 = new byte [2048];
while((len4 = f11.read(b4)) != -1){
f12.write(b4);
}
long t8 = System.currentTimeMillis();
System.out.println(t8-t7);//所用时长 171
总结:从以上四种复制文件的方法来分析高效流与普通流以及按字节读取和写入、按字节数组读取和写入
在性能方面由高到低:高效流数组字节输入输出>普通流字节输入输出>高效流数组字节输入输出>普通流字节输入输出
}
}
字符流方法:
public class Lianxi8 {
publicstatic void main(String[] args) throws IOException {
普通字符流一次一个字符复制文件
long t1 = System.currentTimeMillis();
FileReader f1 = new FileReader("C:\\Users\\Administrator\\Desktop\\java基础知识总结蓝皮书.doc");
FileWriter f2 = newFileWriter("C:\\Users\\Administrator\\Desktop\\代码复制.doc");
int len;
while((len = f1.read()) != -1){
f2.write(len);
}
f1.close();
f2.close();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);//所用时长:230
高效字符流字符数组复制文件
long t3 = System.currentTimeMillis();
FileReader f1 = new FileReader("C:\\Users\\Administrator\\Desktop\\java基础知识总结蓝皮书.doc");
FileWriter f2 = new FileWriter("C:\\Users\\Administrator\\Desktop\\代码复制");
BufferedReader f3 = new BufferedReader(f1);
BufferedWriter f4 = new BufferedWriter(f2);
int len;
char [] c = new char [1024];
while((len = f3.read(c)) != -1){
f4.write(c);
}
f3.close();
f4.close();
long t4 = System.currentTimeMillis();
System.out.println(t4-t3);//所用时长:78
普通字符流字符数组复制文件
long t1 = System.currentTimeMillis();
FileReader f1 = new FileReader("C:\\Users\\Administrator\\Desktop\\java基础知识总结蓝皮书.doc");
FileWriter f2 = newFileWriter("C:\\Users\\Administrator\\Desktop\\代码复制");
int len;
char [] c = new char[1024];
while((len = f1.read(c)) != -1){
f2.write(c);
}
f1.close();
f2.close();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);//所用时长:78
高效字符流一个字符复制文件
long t1 = System.currentTimeMillis();
FileReader f1 = new FileReader("C:\\Users\\Administrator\\Desktop\\java基础知识总结蓝皮书.doc");
FileWriter f2 = newFileWriter("C:\\Users\\Administrator\\Desktop\\代码复制");
BufferedReader f3 = new BufferedReader(f1);
BufferedWriter f4 = new BufferedWriter(f2);
int len;
while((len = f3.read()) != -1){
f4.write(len);
}
f3.close();
f4.close();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);//所用时长:141
高效字符流按行复制文件
long t1 = System.currentTimeMillis();
FileReader f1 = new FileReader("C:\\Users\\Administrator\\Desktop\\java基础知识总结蓝皮书.doc");
FileWriter f2 = newFileWriter("C:\\Users\\Administrator\\Desktop\\代码复制");
BufferedReader f3 = new BufferedReader(f1);
BufferedWriter f4 = new BufferedWriter(f2);
String s;
while((s = f3.readLine()) != null){
f4.write(s);
}
f3.close();
f4.close();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);//所用时长:125
}
}
递归思想复制文件夹:
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Lianxi14 {
//递归复制文件夹
publicstatic void main(String[] args) throws IOException {
File of = new File("C:\\Users\\Administrator\\Desktop\\IOTest");
File nf = new File("C:\\Users\\Administrator\\Desktop\\IOTest复制");
nf.mkdirs();
copyFile(of,nf);
}
publicstatic void copyFile(File oldFile,File newFile) throws IOException{
File[] files = oldFile.listFiles();
for(Filea:files){
if(a.isFile()){//是文件
FileInputStreamf1 = new FileInputStream(a); //创建输入流对象
Filefnew = new File(newFile,a.getName());//创建要复制的同名文件实例
fnew.createNewFile();//创建文件
FileOutputStreamf2 = new FileOutputStream(fnew);//创建输出流对象
intlen;//定义读文件时返回的读取长度
byte[] b = new byte [2048];//定义存储数据的数组
while((len= f1.read(b)) != -1){//读原文件
f2.write(b,0, len);//向同名新文件写出数据
}
f1.close();//关闭流应该写在while循环之外,否则在对一个文件还没读取完就关闭流,文件就不能继续读取
f2.close();
}
else{//是文件夹
//递归调用该方法
Fileff = new File(newFile,a.getName());
ff.mkdirs();
copyFile(a,ff);
}
}
}
}