Copying file using FileStreams in Java
We can copy a file from one location to another using FileInputStream and FileOutputStream classes in Java.
For this we have to import some specific classes of java.io package. So for instance let us include the entire package with statement import java.io.*;
The main logic of copying file is to read the file associated to FileInputStream variable and write the read contents into the file associated with FileOutputStream variable.
Methods used in the program
- int read(); Reads a byte of data. Present in FileInputStream. Other versions of this method : int read(byte bytearray) and int read(byte bytearray, int offset, int length)
- void write(int b) : Writes a byte of data. Present in FileOutputStream. Other versions of this method : void write(byte bytearray) and void write(byte bytearray, int offset, int length);
This article is contributed by Parul Dang. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.