The contents of one file can be copied to another file using byte-oriented streams. The FileInputStream class is used to read data from the source file, while FileOutputStream is used to write that data into the destination file.
- FileInputStream reads file data as bytes, making it suitable for copying different types of file content.
- FileOutputStream writes the bytes read from the source file into the destination file.
- The File class is used to represent the source and destination file paths.
Examples:
Input: sourceFile = "sourcefile.txt", destinationFile = "destinationfile.txt"
Output: File CopiedInput: sourceFile = "data.txt", destinationFile = "backup.txt"
Output: File CopiedInput: sourceFile = "image.jpg", destinationFile = "image_copy.jpg"
Output: File Copied
Classes Used to Copy File Content
Java provides several classes for reading and writing file data during the copying process. FileInputStream reads bytes from the source file, while FileOutputStream writes those bytes to the destination file.
1. FileInputStream Class
FileInputStream is a byte input stream used to read data from a file. It provides methods for reading individual bytes or groups of bytes from the source file.
Syntax
FileInputStream fin = new FileInputStream(filename);
Methods Used
- read(): Reads a byte of data from the input stream. It returns the byte as an integer and returns -1 when the end of the file is reached.
- close(): Closes the input stream and releases the associated resources.
2. FileOutputStream Class
FileOutputStream is a byte output stream used to write data to a file. It receives the bytes read from the source file and writes them to the destination file.
Syntax
FileOutputStream fout = new FileOutputStream(filename);
Methods Used
- write(): Writes a byte of data to the output stream.
- close(): Closes the output stream and releases the associated resources.
3. File Class
The File class is used to represent a file or directory path. It can be used to provide the source and destination paths to the input and output streams.
Syntax
File f = new File(filename);
Note: Creating a File object does not create a physical file. The destination file is created when FileOutputStream opens the specified output file if it does not already exist.
Approach
- Take the source file name from the user.
- Create a File object for the source file.
- Take the destination file name from the user.
- Create a File object for the destination file.
- Create FileInputStream and FileOutputStream objects.
- Read the source file byte by byte using read().
- Write each byte to the destination file using write().
- Close both streams after the copying operation is completed.
import java.io.*;
import java.util.*;
public class CopyFromFileaToFileb {
public static void copyContent(File a, File b)
throws Exception
{
FileInputStream in = new FileInputStream(a);
FileOutputStream out = new FileOutputStream(b);
try {
int n;
// read() function to read the
// byte of data
while ((n = in.read()) != -1) {
// write() function to write
// the byte of data
out.write(n);
}
}
finally {
if (in != null) {
// close() function to close the
// stream
in.close();
}
// close() function to close
// the stream
if (out != null) {
out.close();
}
}
System.out.println("File Copied");
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
// get the source file name
System.out.println(
"Enter the source filename from where you have to read/copy :");
String a = sc.nextLine();
// source file
File x = new File(a);
// get the destination file name
System.out.println(
"Enter the destination filename where you have to write/paste :");
String b = sc.nextLine();
// destination file
File y = new File(b);
// method called to copy the
// contents from x to y
copyContent(x, y);
}
}
Output
Enter the source filename from where you have to read/copy :
sourcefile.txt
Enter the destination filename where you have to write/paste :
destinationfile.txt
File Copied
Explanation: The program reads the contents of sourcefile.txt byte by byte using FileInputStream. Each byte is then written to destinationfile.txt using FileOutputStream, resulting in a copy of the source file.
Source File: The source file contains the original content that will be copied to the destination file.

Destination File: After successful execution, the same content is written into the destination file.
