java實現檔案copy
阿新 • • 發佈:2019-01-03
話不多說直接看程式碼:
方法1:直接利用FileInputStream,FileOutputStream來實現內容的copy
public static void listDicTory(File src, File dest) throws IllegalAccessException, IOException {
/*此處用來判斷檔案是否存在*/if (src.exists() && dest.exists()) {
/*此處用來判斷檔案是否是檔案型別*/ if (src.isFile() && dest.isFile()) {
/*此處用來宣告需要copy的檔案*/ FileInputStream input = new FileInputStream(src);
/*此處用來宣告需要copy到的檔案*/ FileOutputStream out = new FileOutputStream(dest,false);
/*此處用來定義一個緩衝區存放讀取的檔案*/ byte buf[]=new byte[8*1024];
int b;
/*此處用來判斷檔案是否讀完,注意點
(b=input.read(buf,0,buf.length))!=-1:表示讀取到buf緩衝區,從檔案的0號位開始讀取,最多隻能暫存buf.length個位元組
*/ while((b=input.read(buf,0,buf.length))!=-1) {
/*此處用來將緩衝區的內容寫入到out對應的檔案*/ out.write(buf, 0, b);
out.flush();//最好加上
//關閉(close)輸出流時,應先重新整理(flush)換衝的輸出流,話句話說就是:“迫使所有緩衝的輸出資料被寫出到底層輸出流中”。
}
input.close();
out.close();
} else {
throw new IllegalArgumentException("有一個或者多個檔案檔案是資料夾") ;
}
} else {
throw new IllegalArgumentException("有一個或者多個檔案檔案不存在");
}
}
方法2:利用BufferedInputStream,BufferedOutputStream緩衝區來實現內容的copy
public static void listDicTory(File src,File dest) throws IllegalAccessException, IOException {
/*此處用來判斷檔案是否存在*/if (src.exists() && dest.exists()) {
/*此處用來判斷檔案是否是檔案型別*/ if (src.isFile() && dest.isFile()) {
/*此處用來宣告需要copy的檔案*/FileInputStream input = new FileInputStream(src);
/*此處用來將讀取的檔案先放入緩衝區*/BufferedInputStream bis=new BufferedInputStream(input);
/*此處用來宣告需要copy到的檔案*/ FileOutputStream out = new FileOutputStream(dest,false);
/*此處用來將即將要寫入的檔案先放入緩衝區*/BufferedOutputStream bos=new BufferedOutputStream(out);
/*此處用來定義一個緩衝區存放讀取的檔案*/ byte buf[]=new byte[8*1024];
int b;
/*此處用來判斷檔案是否讀完,注意點
(b=bis.read()))!=-1:表示讀取到bis緩衝區的還沒有讀完
*/ while((b=bis.read())!=-1) {
/*此處用來將緩衝區的內容寫入到bos對應的緩衝區*/ bos.write(b);
bos.flush();//此處必須加上,用於重新整理緩衝區,關閉(close)輸出流時,應先重新整理(flush)換衝的輸出流,話句話說就是:“迫使所有緩衝的輸出資料被寫出到底層輸出流中”。
}
bis.close();
bos.close();
} else {
throw new IllegalArgumentException("有一個或者多個檔案檔案是資料夾");
}
} else {
throw new IllegalArgumentException("有一個或者多個檔案檔案不存在");
}
}
方法3:使用InputStreamReader&OutputStremWriter完成copy功能,但是有一個致命的缺點就是無法更改字元編碼
FileInputStream in=new FileInputStream(file);
InputStreamReader inReader=new InputStreamReader(in);
FileOutputStream out=new FileOutputStream(file2,true);//true:不覆蓋原來內容,false:覆蓋原來內容
OutputStreamWriter outWriter=new OutputStreamWriter(out, "utf-8");
int c;
while((c=inReader.read())!=-1) {
outWriter.write((char)c);
outWriter.flush();
}
System.out.println("複製完成");
inReader.close();
outWriter.close();
方法4:FileReader&&FileWriter
public static void listDicTory2(File file,File file2) throws IllegalAccessException, IOException {
FileReader fr=new FileReader(file);
FileWriter fw=new FileWriter(file2,true);//true:不覆蓋原來內容,false:覆蓋原來內容
int c;
while((c=fr.read())!=-1) {
fw.write((char)c);
fw.flush();
}
fr.close();
fw.close();
}
方法5:BufferedReader&&BufferedWriter實現檔案copy的功能
public static void listDicTory2(File file1,File file2) throws IllegalAccessException, IOException {
FileInputStream inputStream=new FileInputStream(file1);
InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
//下面用於定義BufferedWriter
FileOutputStream outputStream=new FileOutputStream(file2);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream, "utf-8");
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);
String line;
while((line=bufferedReader.readLine())!=null) {
bufferedWriter.write(line);
//由於BufferedReader以及BufferedWriter不具有換行功能,所以手動新增換行
bufferedWriter.newLine();
bufferedWriter.flush();//此處必須有flush
}
bufferedReader.close();
bufferedWriter.close();
System.out.println("複製完成");
}
//下面是測試功能
public static void main(String[] args) throws IllegalAccessException, IOException {
File src=new File("C:\\Users\\17732\\Desktop\\test\\testFile.txt");
File dest=new File("C:\\Users\\17732\\Desktop\\test\\testFile(2).txt");
listDicTory(src,dest);
}