Java treats files as streams of characters. FileWriter pushes text out; BufferedReader pulls it back in, one comfortable line at a time.
1 FileWriter writes
new FileWriter("note.txt") opens (or creates) a file and hands you a stream to write into.
2 BufferedReader reads
readLine() returns the next line, or null at end of file — the natural loop condition.
3 Checked exceptions
File operations can fail, so the compiler demands a catch or a throws on the method.
4 close() is mandatory
Closing flushes buffers and releases the file handle — always close, even after a crash.
Streams to disk and back
FileWriter writes1new FileWriter("note.txt") opens (or creates) a file and hands you a stream to write into. BufferedReader reads2readLine() returns the next line, or null at end of file — the natural loop condition.
Checked exceptions3File operations can fail, so the compiler demands a catch or a throws on the method. close() is mandatory4Closing flushes buffers and releases the file handle — always close, even after a crash. 4 building blocks make up Streams to disk and back
The classic read loop: pull lines until readLine() says null, then close.
process decision input / output start / end One program, one round trip: write into note.txt, close it, then read the first line back.
mainmain begins — throws IOException says: file operations can fail, and this method passes that problem up.
Console
output appears as the program runs…
A BufferedReader is a wrapper around a FileReader — the arrow shows who actually touches the file.
Drag to orbit the scene. Press Play to watch memory in action.
todo.txt on disk contains exactly one line: buy milk
Your predicted output — one line per console line
Hint: readLine() returns "buy milk"; toUpperCase() returns a NEW string in capitals — strings are immutable.
Match each piece to its job.
5 conceptsTap a card to expand ?FileWriterdrops into place
?BufferedReaderdrops into place
?readLine()drops into place
?close()drops into place
?IOExceptiondrops into place
01new FileWriter("f.txt") creates or truncates the file and opens it for writing.
02BufferedReader.readLine() is the workhorse: next line, or null at end of file.
03File APIs throw checked IOException — catch it or declare throws on the method.
04Always close your streams; unclosed handles leak files and lose buffered data.
05Writers buffer: text reaches the disk when the stream is flushed or closed.