Copy File in Java: Explanation & Practice
Copy contents from one file to another
Problem summary
Copy the contents of source.txt to destination.txt.
Starter code
import java.io.*;
public class Main {
public static void main(String[] args) {
// Copy source.txt to destination.txt
// Print: Copy successful
}
}Expected output and test cases
- File copied
Copy successful
Hints
- Open reader for source, writer for dest
- Read and write in loop
- Or use Files.copy() for simplicity
Related File I/O exercises
- Practice Append to File in Java
- Practice Count Lines in File in Java
- Practice Check File Exists in Java
Practice all File I/O exercises · Run this idea in the Java compiler