Reputation: 15615
I know that the FileReader
and FileWriter
reads and writes characters one-by-one. So, it should me making a system call everytime.
But my question is that, does it internally use a buffer to optimize IO performance?
Otherwise, reading an array of characters like here,
public int read(char[] cbuf, int offset, int length)
would require a system call for each of the characters being read.
Or writing a portion of String like here,
public void write(String str, int off,int len)
would be quite heavy isn't it?
Upvotes: 1
Views: 108
Reputation: 111219
FileReader
and FileWriter
derive from InputStreamReader
and OutputStreamWriter
. The implementations of these do use buffers internally to make conversions between bytes and characters more efficient, for example OpenJDK uses 8kB buffers (see the StreamDecoder and StreamEncoder classes). The API specification gives you no guarantees though and recommends that you use BufferedReader
and BufferedWriter
for IO performance.
Upvotes: 4
Reputation: 3785
No Aritra. It doesnt use internal buffer. That job is done by BufferedReader. BufferedReader internally buffers the reading of characters. It reads text from a character input stream/file system, and stores it into a buffer. So when a user make a request for next character it will return that character from the buffer , not from the file system. You can wrap BufferedReader around FileReader :
FileReader fr = new FileReader("test.txt");
BufferedReader reader = new BufferedReader(fr);
Upvotes: 0
Reputation: 310850
No, but they respectively use InputStreamReader
and OutputStreamWriter
, which both have buffers.
Upvotes: 1