JAva書き込みファイル読み書き操作(IOストリーム、文字ストリーム)

996 ワード



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(); } } }