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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java.io.File Class in Java
- Delete a file using Java
- File Permissions in Java
- Java | Renaming a file
- Java Class File
- How to run java class file which is in different directory?
- Java | Appending String to a File
- How to play an Audio file using Java
- Check if a File is hidden in Java
- Size of file on the Internet using Java
- Moving a file from one directory to another using Java
- Reading a CSV file in Java using OpenCSV
- Writing a CSV file in Java using OpenCSV
- Myth about the file name and class name in Java
- File length() method in Java with Examples
- Convert byte array to File using Java
- File setExecutable() method in Java with Examples
- File canExecute() method in Java with Examples
- File setReadable() function in Java with Examples
- File createNewFile() method in Java with Examples