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{fileinputstream in = null; FileOutputStream fs = null;try {///instantiate the file and determine if the file is present in document Oldfile=new file (oldpth); if (Oldfile.exists ()) {//Initialize file input and output stream in= New FileInputStream (oldpth); fs=new FileOutputStream (Newpath,add);//define an array to hold read data byte[] buffer=new byte[10];int length ; while (true) {int len=in.read (buffer);//When the file is read, return 1, otherwise read the file length/* Note: The output reads the current file content method * String S=new string (buffer); * S.trim ();(Remove the spaces at the front and back of the string) */if (len==-1) break;fs.write (buffer); System.out.println ("OK");}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{in.close (); Fs.close ();}}}
Java write file read/write operation (IO stream, byte stream)