1. 程式人生 > >文件的復制操作

文件的復制操作

作文 方式 oid ade create time() main 出現 input

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);//處理時間

}


}

文件的復制操作