Simple File Encryption in Java: Explanation & Practice

Encrypt file contents using Caesar cipher

Problem summary

Read a file, encrypt using Caesar cipher (shift each letter by 3), write to output.

Starter code

import java.io.*;

public class Main {
    public static void main(String[] args) {
        int shift = 3;
        // Read input.txt
        // Encrypt with Caesar cipher
        // Write to encrypted.txt
        // Print: Encryption complete
    }
}

Expected output and test cases

  • File encrypted
    Encryption complete
  • Simple shift
    ABC -> DEF
  • Wrap around
    XYZ -> ABC

Hints

  1. Handle uppercase and lowercase separately
  2. Wrap around at Z/z
  3. Non-letters stay unchanged

Related File I/O exercises

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