package copyfile;
import java.io.*;
public class copy {
public static void main(String[] args) throws IOException {
copyFile("d:/new/a.txt","d:/new/b.txt",true);//oldpath,newpath,是否不覆寫前文
}
public static void copyFile(String oldpth,String newpath,boolean add) throws IOException{
FileReader fr = null;
FileWriter fw = null;
try {
//執行個體化檔案,并判斷檔案是否存在
File oldfile=new File(oldpth);
if(oldfile.exists()){
//初始化檔案輸入與輸出流
fr=new FileReader(oldpth);
fw=new FileWriter(newpath,add);
//定義存放讀取資料的數組
char[] buffer=new char[10];
int length;
while(true){
int len=fr.read(buffer);//當檔案讀完,傳回-1,否則傳回讀取檔案長度
if(len==-1)break;
fw.write(buffer);
}
System.out.println("OK");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
fr.close();
fw.close();
}
}
}