Package Copyfile;import java.io.*;p ublic class Copy {public static void main (string[] args) throws IOException {CopyFile ( "D:/new/a.txt", "D:/new/b.txt", true);//oldpath,newpath, does not overwrite the preceding}public static void CopyFile (String oldpth,string Newpath,boolean add) throws Ioexception{filereader fr = null; FileWriter FW = null;try {///instantiate the file, and determine if the file exists with the files Oldfile=new file (oldpth); if (Oldfile.exists ()) {//Initialize file input and output stream fr=new FileReader (oldpth); fw=new FileWriter (Newpath,add);//define an array to hold read data char[] buffer=new char[10];int Length;while (True) { int len=fr.read (buffer);//When the file is read, return-1, otherwise return the read file length if (len==-1) break;fw.write (buffer);} System.out.println ("OK");}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{fr.close (); Fw.close ();}}}
Java write file read and write operations (IO stream, character stream)