1
Often we find codes that could be solved with the use of objects of the type FileWriter
or FileOutputStream
. For example, a simple code that writes "Teste
" in the archive /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, the 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?
An interesting link on the subject: Java file writing: Filewriter and Fileoutputstream
– user28595