Byte stream, character stream involves more analogy, more easily confused. Therefore, it is necessary to make a generalization about when to use a byte stream, when to use a character stream, and when to use a stream of the buffer class. To generalize them, you don't need too many languages, just grab their focus and features.
Here are a few questions to consider when deciding when to use which class.
- Whether there is a data source, whether the flow of data has a target.
- Data source: represents input, or read. The two parent classes available for use are inputstream and reader.
- Target: Represents the output, or is called write. The two parent classes available for use are OutputStream and writer.
- Should I use a byte stream or a character stream? If the source or destination contains non-ASCII characters, a character stream is used.
- What device type the source and destination are.
- Source: Disk File, Memory (Byte/character array), keyboard system.in, network socket
- Target: Disk file, Memory (Byte/character array), screen System.out, network socket
- Do I need to use Bufferedreader/bufferedwriter? This will need to consider whether additional special features are required, including operation Lines, character set conversions, and the use of buffers to improve efficiency . The process of converting a byte stream into a character stream is involved in character set conversion, and it may be necessary to use InputStreamReader and OutputStreamWriter as a bridge for conversion.
Finally, it is necessary to know that for an input stream using BufferedReader, it is sometimes possible to consider using a character array for better results and performance.
Here is an example of a requirement to apply the above rules: read the file data that contains GBK Simplified Chinese and copy it to another file with Utf-8 encoding.
1. The source has a target, and all are files. 2. Both read and write contain Chinese characters, so a character stream is used. 3. Transcoding is required during the write process, so you need to use OutputStreamWriter. 4. You can use the buffer function to improve efficiency.
Importjava.io.*; Public class CP {Public static void main(string[] args) throws IOException{File src =NewFile ("D:/myjava/a.txt"); File dest =NewFile ("D:/myjava/a_bak.txt"); CP (Src,dest); }Public static void cp(File src,file dest) throws IOException{BufferedReader BUFR =NewBufferedReader (NewFileReader (SRC)); BufferedWriter BUFW =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (dest),"Utf-8"));//Read by lineString line =NULL; while((Line=bufr.readline ())! =NULL) {Bufw.write (line); Bufw.newline (); Bufw.flush (); } bufw.close (); }}
After the above code executes, the end of the target file will be more than a blank line in the source file, the above method is not a good solution to this problem. However, if you use a character array instead of BufferedReader, there is no such problem, as follows.
Importjava.io.*; Public class CP {Public static void main(string[] args) throws IOException {File src =NewFile ("D:/myjava/a.txt"); File dest =NewFile ("D:/myjava/a_bak.txt"); CP (Src,dest); }Public static void cp(File src,file dest) throws IOException{FileReader FR =NewFileReader (SRC); BufferedWriter BUFW =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (dest),"Utf-8"));Char[] buf =New Char[1024x768];intLen =0; while((Len=fr.read (BUF))!=-1) {Bufw.write (buf,0, Len); Bufw.flush (); } bufw.close (); }}
Note: If you think this article is not bad please click on the lower right corner of the recommendation, your support can inspire the author more enthusiasm for writing, thank you very much!
Java IO (v): selection rule of Byte stream and character stream