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

  1. Open reader for source, writer for dest
  2. Read and write in loop
  3. Or use Files.copy() for simplicity

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler