[Simple Java] FileOutputStream VS FileWriter, javafileoutputstream
When we use Java to write data to a file, we can use FileOutputStream or FileWriter in two ways.
FileOutputStream:
File fout = new File (file_location_string); FileOutputStream fos = new FileOutputStream (fout); BufferedWriter out = new BufferedWriter (new OutputStreamWriter (fos); out. write ("something ");
FileWriter:
FileWriter fstream = new FileWriter (file_location_string); BufferedWriter out = new BufferedWriter (fstream); out. write ("something ");
The above two methods can run normally, but what is the difference between them?
There are too many of the above two classes that need to be discussed. They all implement the file I/O concept of common operating systems. However, we do not care about how they are designed, how they are selected, and why they are selected.
The Java API specification describes them as follows:
FileOutputStream is suitable for writing raw bytes of data, such.
FileWriter is suitable for writing streams.
If you are familiar with the design mode, FileWriter is a typical application of the decoration mode. I have a simple tutorial on the decorator mode, because it is very useful and applied to many designs.
In addition, FileOutputStream can convert files into byte arrays.
Http://www.programcreek.com/2011/03/fileoutputstream-vs-filewriter/.