Conversion stream:
The code for reading the keyboard input above is similar to the bufferedreader Readline () method. Can it be replaced by Readline? Because system. In is a byte stream, it needs to use the swap stream buffer method, which requires two conversion streams: inputstreamreader and outputstreamwriter.
Package Tan; import java. Io. *; public class transtreamdemo {public static void main (string [] ARGs) throws ioexception {/* // gets the keyboard input object. Inputstream in = system. In; // convert a byte stream object to a bytes Stream object and use the conversion stream. Inputstreamreaderinputstreamreader ISR = new inputstreamreader (in); // to improve efficiency, use the string for efficient buffer operations. Use bufferedreaderbufferedreader BR = new bufferedreader (ISR); * // the most common method of writing a keyboard. [Note] bufferedreader bufr = new bufferedreader (New inputstreamreader (system. in); // outputstream out = system. out; // outputstreamwriter OSW = new outputstreamwriter (out); // bufferedwriter bufw = new bufferedwriter (OSW); bufferedwriter bufw = new bufferedwriter (New outputstreamwriter (system. out); string Len = NULL; while (LEN = bufr. readline ())! = NULL) {If ("over ". equals (LEN) {break;} bufw. write (Len. touppercase (); bufw. newline (); // The line feed operation of the buffer bufw. flush (); // refresh the buffer} bufr. close (); bufw. close ();}}
Basic flow operation rules
The biggest headache for I/O operations is how to select the desired class in its system. This process can be completed in three steps:
1. Clarify the source and purpose.
Source: input stream, inputstream and reader;
Objective: To output streams, outputstream, and writer
2. Check whether the operated data is plain text.
Yes: Batch stream
No: byte stream.
3. When the system is clear, the specific object to be used will be clarified.
Distinguish between devices:
Source Device: memory, hard disk, keyboard
Target devices: memory, hard disk, and console.
Example:
1. store data in one text file to another. Copy a file.
Source: Read stream is used because it is a source. Inputstream Reader
Is it a text file operation?
Yes! Then you can select Reader
In this way, the system is clear.
Next, specify the object in the system.
Device Identification: Hard disk. The previous file.
In the reader system, the object that can operate files is filereader.
Whether to improve efficiency: Yes !. Added to the buffer zone bufferedreader in the reader system.
Filereader Fr = new filereader ("a.txt ");
Bufferedreader bufr = new bufferedreader (FR );
Purpose: Outputstream writer
Is it plain text?Yes! Writer.
Device: hard disk, a file.
The object filewriter that can operate files in the writer system.
Whether to improve efficiency: Yes !. Buffer bufferedwriter in writer System
Filewriter fw = new filewriter ("B .txt ");
Bufferedwriter bufw = new bufferedwriter (FW );
2. Requirement: Save the data entered by the keyboard to a file.
If both source and target exist in this requirement, analyze them separately.
Source: Inputstream Reader
Is it plain text? Yes! Reader
Device: keyboard. The corresponding object is system. In.
Didn't you select reader? Does system. In correspond to a byte stream?
To facilitate the operation of text data on the keyboard. It is most convenient to convert a stream into a string.
Therefore, since reader is clearly defined, system. In is converted to reader.
Using the reader system to convert the stream, inputstreamreader
Inputstreamreader ISR = new inputstreamreader (system. In );
Need to improve efficiency? Yes! Bufferedreader
Bufferedreader bufr = new bufferedreader (ISR );
Purpose: Outputstream writer
Is text stored? Yes! Writer.
Device: hard disk. One file. Use filewriter.
Filewriter fw = new filewriter ("c.txt ");
Need to improve efficiency? Yes.
Bufferedwriter bufw = new bufferedwriter (FW );
Extended: You want to save the input data to the file according to the specified encoding table (UTF-8.
Purpose: outputstream writer
Is text stored? Yes! Writer.
Device: hard disk. One file. Use filewriter.
However, filewriter uses the default encoding table. GBK.
However, during storage, you must add the specified encoding table UTF-8. The specified encoding table can only be specified for the conversion stream.
Therefore, the object to be used is outputstreamwriter.
The conversion Stream object must receive a byte output stream. It can also operate the byte output stream of files. Fileoutputstream
Outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream ("d.txt"), "UTF-8 ");
Need efficiency? Yes.
Bufferedwriter bufw = new bufferedwriter (OSW );
So remember. When to use the conversion stream. A bridge between characters and bytes. Generally, a conversion stream is required when character encoding is involved.
Exception information serialization
Import Java. io. *; import Java. util. *; import Java. text. *; Class exceptioninfo {publicstaticvoid main (string [] ARGs) throws ioexception {try {int [] arr = newint [2]; system. out. println (ARR [3]);} catch (exception e) {try {date d = new date (); simpledateformat SDF = new simpledateformat ("yyyy-MM-DDHH: mm: SS "); strings = SDF. format (d); printstreamps = new printstream (New fileoutputstream ("exception. log" , True); PS. println (s); system. setout (PS);} catch (ioexception ex) {thrownewruntimeexception ("log file creation failed");} e. printstacktrace (system. out) ;}}/ * log4j is used to store Java log sdks on the network. Many objects are available. */
Serialization of system information
Import Java. util. *; import Java. io. *; Class systeminfo {public static void main (string [] ARGs) throws ioexception {properties prop = system. getproperties (); // system. out. println (PROP); // prop. list (system. out); // you do not need to traverse the set like in the set. // You can output the attribute list to the specified output stream prop. list (New printstream ("sysinfo.txt "));}}