Batch stream processingThe Unit is two bytes of Unicode characters.Character, character array, or string, AndByte stream processingUnit: 1 byte, OperationByte and byte array. Therefore, the compaction stream is a character that is converted from a Java virtual machine to a Unicode Character in two bytes. It supports many languages!For audio files, images, and songs, use words to reduce traffic.,If it is related to Chinese (text), it is better to use the pipeline stream..(If the input is an integer, whenCompositionThis processing)
All files are stored in bytes. What is stored on the disk is not the character of the file, but the characters are first encoded into bytes, and then stored on the disk. When reading a file (especially a text file), it is also a byte read to form a byte sequence.
Byte streams can be used for any type of objects, including binary objects. The byte stream can only process characters or strings;Byte stream provides the ability to process any typeIoBut it cannot be processed directly.UnicodeCharacter.
The byte stream class is used to process characters or strings. When processing bytes or binary objects, the byte stream class is applied.
1. Source: Enter the keyboard.
Purpose: To log on to the console.
2. Requirement: store the data entered by the keyboard into a file.
Source: keyboard.
Purpose: file.
3. Requirement: print the data of a file on the console.
Source: file.
Purpose: To log on to the console.
Basic Rules of stream operations:
Done through three definitions
- Clarify source and purpose
Source: input stream. Inputstream Reader
Purpose: To output a stream. Outputstream writer
- Whether the operated data is plain text
Yes: streams.
No: byte stream.
- When the system is clear, it is clear which specific object to use
Differentiate by devices:
Source Device: memory, hard disk. Keyboard
Target device: memory, hard disk, and console.
1Store data in one text file to another. Copy a file
Source: because it is the source, the read stream is used. Inputstream Reader
Whether to operate a text file.
Yes! Then you can select Reader
In this way, the system is clear.
Next, specify the object in the system.
Device: 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
Whether it is 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 );
Exercise: store data in one image file to another. Copy a file. You must complete the three definitions in the preceding format.
---------------------------------------
2Requirement: Save the data entered by the keyboard to a file.
Both sources and purposes exist in this requirement.
Separate Analysis
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: outputstreamwriter
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 follow the specified encoding table (UTF-8) To save the data to the file.
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 );
Remember when to use the conversion stream. The bridge between characters and bytes. Generally, when character encoding is involved,
The conversion stream is required.
Import java. Io .*;
Class
Transstreamdemo2
{
Public
Static void main (string [] ARGs) throws ioexception
{
System. setin (New
Fileinputstream ("persondemo. Java "));
System. setout (New
Printstream ("zzz.txt "));
// The most common keyboard writing method.
Bufferedreader
Bufr = new bufferedreader (New inputstreamreader (system. In ));
Bufferedwriter
Bufw = new bufferedwriter (New outputstreamwriter (system. Out ));
String
Line = NULL;
While (line = bufr. Readline ())! = NULL)
{
If ("over". Equals (line ))
Break;
Bufw. Write (line. touppercase ());
Bufw. newline ();
Bufw. Flush ();
}
Bufr. Close ();
}
}