package zy10_09;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
* 完成复制操作(操作文件wmv/mp4)
需要计算操作时间。(超过1G以上文件)
复制方式至少3种,其中有一种,效率最高,有一种出现乱码或者文件复制后无法正常使用,另外一种,正常使用,但效率低下
InputStream Reader
OutputStream Writer
*/

public class Copy {

public static void main(String[] args) throws FileNotFoundException {



//用BufferedInputStream和BufferedOutputStream处理
long t1=System.nanoTime();//处理前的时间
//关联两个文件
File file01=new File("E:\\僵尸赌王.bhd");
File file02=new File("E:\\僵尸赌王fuzhi2.bhd");

//判断并创建file02,
try {
if(file02.createNewFile()) {
System.out.println("[成功创建]");
}else {
System.out.println( "[文件存在-不覆盖创建]");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

FileInputStream fi=null;
BufferedInputStream bi=null;
FileOutputStream fo=null;
BufferedOutputStream bo=null;

try {
fi = new FileInputStream(file01);
bi = new BufferedInputStream(fi);
fo = new FileOutputStream(file02);
bo = new BufferedOutputStream(fo);

int line =bi.read();
while(line!=-1) {
//System.out.println(line);
bo.write(line);
line=bi.read();
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fi!=null) {
try {
fi.close();
bo.flush();
bo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long t2=System.nanoTime();//处理前的时间,以毫微秒为单位
long shijian1=(t2-t1)/1000000;//转换成秒为单位
System.out.println(shijian1);//处理时间


//用InputStream和OutputStream,

long t3=System.nanoTime();//处理前的时间
//关联两个文件
File file03=new File("E:\\摔跤吧!爸爸.bhd");
File file04=new File("E:\\摔跤吧!爸爸fuzhi1.bhd");


//判断并创建file02,
try {
if(file04.createNewFile()) {
System.out.println("[成功创建]");
}else {
System.out.println( "[文件存在-不覆盖创建]");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileInputStream fr = new FileInputStream(file03);
FileOutputStream fw=new FileOutputStream(file04);

//从此输入流中将 byte.length 个字节的数据读入一个 byte 数组中。
byte[] buf=new byte[1024];
while(fr.read(buf)!=-1) {
//String str = new String (buf);
fw.write(buf);
//System.out.println(str);
}

fw.flush();
fw.close();
fr.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long t4=System.nanoTime();//处理前的时间,以毫微秒为单位
long shijian2=(t4-t3)/1000000;//转换成秒为单位
System.out.println(shijian2);//处理时间



//用Reader和Writer方法
long t5=System.nanoTime();//处理前的时间,以毫微秒为单位
//关联两个文件
File file05=new File("E:\\僵尸赌王.bhd");
File file06=new File("E:\\僵尸赌王fuzhi1.bhd");


//判断并创建file02,
try {
if(file06.createNewFile()) {
System.out.println("[成功创建]");
}else {
System.out.println( "[文件存在-不覆盖创建]");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileReader fr=new FileReader(file05);
FileWriter fw=new FileWriter(file06);
int i=fr.read();
while(i!=-1) {
fw.write((char)i);
i=fr.read();
}
fw.flush();
fw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

long t6=System.nanoTime();//处理前的时间,以毫微秒为单位
long shijian3=(t6-t5)/1000000;//转换成秒为单位
System.out.println(shijian3);//处理时间

}


}