关于Java中BufferedReader与FileReader协同工作原理的疑问
I’ve been digging into how BufferedReader and FileReader collaborate in Java for three whole days now. I’ve scoured Stack Overflow posts and Google searches, but I’m still stuck on a few key points that just aren’t clicking for me. Let me break down my questions:
For the common code line
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
My initial take was thatFileReaderreads data byte by byte and feeds it into the buffer thatBufferedReadersets up, then theBufferedReaderinstance reads from that buffer. But I found a Stack Overflow post called Understanding how BufferedReader works in Java that saidBufferedReaderitself doesn’t store any data. That’s throwing me off—doesBufferedReaderboth create the buffer and then act as the reader for that buffer? That feels contradictory, and I can’t wrap my head around how that makes sense.I get that
BufferedReaderis supposed to cut down on expensive I/O operations by reading large chunks of data at once, instead of repeatedly reading single bytes from disk and converting them to characters. But whenBufferedReaderwraps aFileReader, isFileReaderthe one doing the initial reading and passing data toBufferedReader? If that’s the case, how doesBufferedReadermanage to read those large chunks instead of the tiny byte reads thatFileReadernormally does?I thought the big win with
BufferedReaderwas that it reads from an in-memory buffer, skipping the overhead of reading directly from disk and converting on the fly. It seems logical that loading a big block into memory first and then processing it would be faster, but I’m not fully buying it—none of the actual steps (reading bytes, converting to characters) are skipped, so why does this approach save time?
I’m really confused here and would love a clear, precise explanation that helps me finally get this straight.
内容的提问来源于stack exchange,提问作者Andrea

