Search code examples
javaiofilereaderfilewriter

Why is there no FileReader.read(String), when FileWriter has write(String)


I have noticed that in FileWriter there is a method to write a String: .write(String), whereas in FileReader there is only the possibility to read into a array of char.

Is there a reason behind it?


Solution

  • The String class is immutable so it would be impossible to "read into a String". On the other hand, if you just meant String read(), then the problem there is that you haven't communicated the size of the string you wish to read. Yes, there could be a method String read(int), which would say how much you want to read at once, but since the step from char[] to String is very simple, there was no need for a second method like that. read(char[]), on the other hand is a much more versatile – and efficient – method.