1. Clarify the source and purpose
Source: input stream
Inputstream
Reader
----------------
Objective: To output a stream
Outputstream
Writer
2. Whether the operated data is plain text
Yes: Batch stream
No: byte stream
3. When the system is clear, specify the specific object to use.
Distinguish between source devices:
1. Memory
2. Hard Disk
3. keyboard
Target device:
1. Memory
2. Hard Disk
3. Console
--------------------------------------------------
1. Copy an object
Source: because it is the source, the read stream is used. Inputstream Reader
Is it a text file operation?
Yes, then you can choose reader, so that the system is clear. ,
Next, identify the object in the system,
Device: hard disk, the last file.
In the reader system, the object that can operate files is filereader.
Filereader Fr = new filereader(“a.txt ");
Need to improve efficiency? Yes. Join the buffer zone in the reader system.
Bufferedreader bufr = new bufferedreader (FR );
Purpose: outputstream writer
Whether it is plain text
Yes. Writer
Device: hard disk, a file.
In the writer system, filewriter can operate files.
Filewriter fw = new filewriter(“ B .txt ");
Need to improve efficiency? Yes, just join the buffer in the writer System
Bufferedwriter bufw = new bufferedwriter (FW );
--------------------------------
Requirement: Save the data entered by the keyboard to a file
Source: inputstream Reader
1. Is it plain text? Yes, Reader
2. device? Keyboard. The corresponding object is system. In.
Didn't you select reader? Does system. In correspond to a byte stream?
In order to operate the text data of the keyboard conveniently, it is most convenient to convert the data into a text stream according to the string. Therefore, since reader is clearly defined, system. In is converted to reader.
Reader System Conversion stream is used. Inputstreamreader
Inputstreamreader ISR = new inputstreamreader (system. In );
Need to improve efficiency?
Bufferedreader bufr = new bufferedreader (ISR );
Purpose: outputstream writer
Is text stored? Yes, writer
Device? Hard disk, a file-> filewriter
Filewriter fw = new filewriter()a.txt ");
Need to improve efficiency? Yes
Bufferedwriter bufw = new bufferedwriter (FW );
--------------------------------------------------------------------
Expand to store the entered data according to the specified encoding.
Purpose:
Save text? Yes, writer
Device: hard disk, a file filewriter. However, filewriter uses the default encoding table. Encoding must be specified during storage. outputstreamwriter can specify encoding.
Outputstreamwriter OSW = new outputstreamwriter (New fileoutputstream(mongod.txt "," UTF-8 "));
Need efficiency?
Bufferedwriter bufw = new bufferedwriter (OSW );
So remember, when to use the conversion stream, the bridge between characters and bytes. Generally, the conversion stream is required when character encoding conversion is involved.