+ 1

Need Help....

How I Copy The Data From One File And Then Paste It To Another File Using Java Input Output???

2nd Feb 2017, 3:41 PM
Vijay Mehta
1 Answer
+ 1
To copy content of one file to another file in java. First we can read the file using FileInputStream and then we can write the read content to the output file using FileOutputStream. Example The below code would copy the content of “MyInputFile.txt” to the “MyOutputFile.txt” file. If “MyOutputFile.txt” doesn’t exist then the program would create the file first and then it would copy the content. import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyExample { public static void main(String[] args) { FileInputStream instream = null; FileOutputStream outstream = null; try{ File infile =new File("C:\\MyInputFile.txt"); File outfile =new File("C:\\MyOutputFile.txt"); instream = new FileInputStream(infile); outstream = new FileOutputStream(outfile); byte[] buffer = new byte[1024]; int length; /*copying the contents from input. stream to output stream using read and write methods */ while ((length = instream.read(buffer)) > 0){ outstream.write(buffer, 0, length); } //Closing the input/output file streams instream.close(); outstream.close(); System.out.println("File copied successfully!!"); }catch(IOException ioe){ ioe.printStackTrace(); } } } Hope it helps! Reference link: http://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/
2nd Feb 2017, 8:42 PM
Kenny Dabiri
Kenny Dabiri - avatar