Quais as vantagens e desvantagens de utilizar o FileWriter sozinho, ou junto com o BufferedWriter?
Quais as vantagens e desvantagens de utilizar o FileWriter sozinho, ou junto com o BufferedWriter?
"In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient."
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html
Breno, nesse caso você incluiu o PrintWriter na questão, embora eu tenha tratado somente do FileWriter e BufferedWriter.
Eu entendo que, quando há muita escrita no arquivo, seja interessante usar o buffer (isso se o código não fizer flush o tempo todo). Mas quando é vantagem usar o FileWriter então?
FileWriter : Convenience class for writing character files. (https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html)
BufferedWriter : Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. (https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html)
PrintWriter :Prints formatted representations of objects to a text-output stream(https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html)
O PrintWriter basicamente auxilia na escrita formatada , e pode receber um Writer no construtor como no caso do exemplo do JAVADOC acima.
Basicamente o FileWriter é uma classe mais baixo nível na hierarquia do pacote JAVA.IO , se muito tráfico de dados , acesso a disco , perfomance no geral não for problema , pode utilizar diretamente.
Obrigado pelas explicações e também pelos links!