Back Home

📄✍️ Java Text I/O 💾🔍

In Java, Scanner reads input from the keyboard or from files (token-by-token using delimiters like whitespace), while PrintWriter writes text out to files cleanly. You’ll usually wrap file I/O in try-catch blocks to handle errors like missing files, and choose between approaches based on the job: Scanner is convenient for parsing numbers/words; PrintWriter is great for writing human-readable text; buffered streams are better for larger data or performance-sensitive work.

Code snippet (write a file) PrintWriter
Simulated file system 3 files
Click a file to preview it. This middle panel is the “directory.”
File contents none
(select a file)

Common problems: FileNotFoundException happens when the path doesn’t exist or your program lacks permission. With Scanner, delimiter and token issues are frequent—using nextInt() when the next token isn’t a number, or mixing nextLine() with next()/nextInt() (newline leftovers). Java file paths can be relative to the “working directory,” so "data/notes.txt" might work in an IDE but fail elsewhere unless the folder exists.

Code snippet (proper error handling) try-catch + throws
Interactive simulator (read → write → close cycle) localStorage
“Save to File” overwrites the file content (like opening a PrintWriter normally). Use “Write New Line” to append.
Ready. Pick a file or type a new filename to create one.
(output appears here)
“Read File” prepares a Scanner-like read. “Next Line” simulates successive nextLine() calls.