Often we find codes that could be solved by using objects of type FileWriter
or FileOutputStream
. For example, a simple code that writes " Teste
" in the file /tmp/arquivo
:
With FileWriter
:
BufferedWriter output = new BufferedWriter(new FileWriter("/tmp/arquivo"););
output.write("Teste");
With FileOutputStream
:
FileOutputStream file = new FileOutputStream(new File("/tmp/arquivo"));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(file));
output.write("Teste");
This happens when we are dealing with characters (another example would be a program that copies content from one file to another). When we are dealing with different files, such as images, FileWriter
is no longer appropriate. But what are the implications of using FileOutputStream
and FileWriter
with character streams? Is one considered better than the other in this case? Is there a difference?