Java File I/O Compiler

Modern Java file I/O uses java.nio.file. The example below writes and reads a file in /tmp — the only writable directory in the sandbox. Edit and run live.

Run Java File I/O Compiler examples in the Java compiler

Examples included

Practical tips

  • Only /tmp is writable in the sandbox. Use Path.of("/tmp/yourfile.txt").
  • Files.writeString and Files.readString are the modern, simplest API (Java 11+).
  • Always wrap file ops in try-with-resources or handle IOException.

Common questions

Can I write files in this online Java compiler?

Yes — to /tmp only. Other paths are read-only in the sandbox.

Does my file persist?

No. The sandbox resets after each run. Files are gone immediately after.

Practice related Java exercises · Browse Java programs with output