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);????????????????????//定義存放讀取數(shù)據(jù)的數(shù)組????????????????????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();????????}????}}
以上就是長沙中公優(yōu)就業(yè)java培訓機構(gòu)的小編針對“Java寫文件讀寫操作之IO流,字符流”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務。