Convert Stream
★ Conversion Flow function 1: acts as a bridge between the stream of bytes and character streams
Requirements: Analog English Chat program, request:
(1) From the keyboard input English characters, each row of it will be converted to uppercase output to the console;
(2) Save the chat record to the byte stream file. Requirements 1 Design analysis:
1 need to receive input from the keyboard, you have to use system.in, it is the byte input stream inputstream;
2 need to deal with characters, you can turn the byte strong to the character, you can also use character streams;
3 requires a function similar to ReadLine, and this method is in the character stream BufferedReader (and the class has buffering growth).
On the other hand, it is reasonable to use the conversion flow to process byte stream into character streams, that is, using InputStreamReader
Requirements 2 Design Analysis:
1 The character data should be saved to the byte stream file by the line;
2 character stream using BufferedWriter is more suitable, because it has newline method and can achieve high efficiency;
3 The word throttling file, you have to use FileOutputStream.
On the other hand, it is more reasonable to use the conversion flow to process the character flow into the word throttling, that is, using OutputStreamWriter
Code implementation:
Package io.transfer;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter; public class Transtreamdemo {public static void main (string[] args) throws IOException {//input inputs
Tream in = system.in;
InputStreamReader ISR = new InputStreamReader (in);
BufferedReader br = new BufferedReader (ISR);
Output OutputStream out = new FileOutputStream ("Chat.txt");
OutputStreamWriter OSW = new OutputStreamWriter (out);
BufferedWriter bw = new BufferedWriter (OSW);
String line = null; while ((Line=br.readline ())!=null) {if (+ over. equals) {//Develop good code habits: When you call a method in string, you place the constant string in the
In front, prevent the variable from being null and cause an abnormal break;
} System.out.println (Line.touppercase ()); Bw. write (line);
Bw.newline ();
Bw.flush ();//character stream is buffered, must brush buffer}}}
Convert Stream
(InputStreamReader and OutputStreamWriter) ★ Conversion Flow function 2: Character encoding conversion
Using FileWriter to encode in the default way
fileoutputstream+ Default Encoding Table
Using a conversion stream to encode by default
OutputStreamWriter + fileoutputstream + default encoding table
Using a transformation stream to encode in a specified encoding
OutputStreamWriter + FileOutputStream + designated coding table
Using a transformation stream to decode the specified encoding
InputStreamReader + fileinputstream + Specify coding table code implementation:
Package io.transfer;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter; public class TranStreamDemo2 {public static void main (string[] args) {try {//readtextdecoding1
();
ReadTextDecoding2 ();
Writetextencoding ();
catch (IOException e) {e.printstacktrace (); } private static void Writetextencoding () throws IOException {//First: filewriter+ default encoding Table Filewrit
ER fw = new FileWriter ("Files\\w_utf-8.txt");/the encoding of the file is determined by the platform (such as MyEclipse or DOS window), not necessarily utf-8 fw.write ("A little progress every day ...");
Fw.close (); The second type: outputstreamwriter+ default encoding table OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("Files\\w_utf -8_2.txt ")//The encoding of the file is determined by the platform (such as MyEclipse or DOS window), not necessarily the Utf-8 OSW.write ("A little progress on the first day ...");//Osw.close (); The third type: outputstreamwriter+ specifies the encoding table OutputStreamWriter OSW2 = new OutputStreamWriter (New FileOutputStream ("Files\\w_ut
F-8_3.txt ")," Utf-8 ");//The encoding of the file must be utf-8, because it is the osw2.write of our own designation (" A little progress on the first day ... ");
Osw2.close (); private static void ReadTextDecoding1 () throws IOException {FileReader fr = new FileReader ("files\\utf-8.t
XT ")//using the default encoding table to decode char[] Cbuf = new CHAR[10];
int len=0;
while ((Len=fr.read (CBUF))!=-1) {String str = new String (Cbuf,0,len);
System.out.print (str);
} fr.close (); private static void ReadTextDecoding2 () throws IOException {//inputstreamreader ISR = new Inputstreamreade
R (New FileInputStream ("Files\\gbk.txt"))//If no encoding table is specified, use the default//conversion stream to specify the decoding table----as long as the encoding table of the file and the decoder table specified here is the same, there will be no garbled InputStreamReader ISR = new InputStreamReader (New FileInputStream ("Files\\gbk.txt"), "GBK"); OK//INPUtstreamreader ISR = new InputStreamReader (New FileInputStream ("Files\\utf-8.txt"), "GBK");/garbled InputStreamReader
ISR = new InputStreamReader (New FileInputStream ("Files\\utf-8.txt"), "Utf-8");//ok char[] cbuf = new CHAR[20];
int len = Isr.read (CBUF);
String str = new string (Cbuf,0,len);
System.out.println (str);
Isr.close ();
}
}
Print Flow
★ The characteristics of the print stream:
1 only the output is not entered. PrintStream is a byte print stream, PrintWriter is a character print stream.
2 can easily print a variety of data "value representation", providing a series of printing functions (only it has, the other flow is not.) )
3), unlike other output streams, it never throws IOException exceptions (except the construction method), resolves internally and sets internal flags.
4 You can create features that have automatic refreshes, and you can use the println () method with line breaks.
5) (in the constructor method) can specify the character set encoding. ★ about automatic refresh of print flow
Autoflush-boolean variable; If true, the println, printf, or Format method refreshes the output buffer.
*-is actually because these methods have helped us call Out.flush ().
Code implementation: //Demo PrintStream class automatic refresh function
Package io.print;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.PrintStream; /** * * * * @author Chen Haoxiang * * * 2016-4-24//Demo PrintStream class automatic refresh feature public class Printstreamdemo {public static void
Main (string[] args) {try {demo1 ();
Demo2 ();
for (int i=0;i<10;i++) {System.out.println (i);
The//screen terminal will not have output because the Demo2 () is running ()///The System.out output destination is changed from the screen to the log file. catch (IOException e) {
E.printstacktrace (); }///change the output destination of System.out from screen to log file private static void Demo2 () throws IOException {FileOutputStream
Font = new FileOutputStream ("Log.txt");
PrintStream out = new PrintStream (font,true);
System.setout (out);
private static void Demo1 () throws IOException {PrintStream out =new printstream ("Print.txt"); Out.write () writes only one byte (binary 8-bit) of information, if the parameteris greater than the range of a byte,//Then actually writes only the last byte of Data out.write (97);
Out.write (353);
The last byte is 97, so the write is a character ' a '----write the value of the Representation System.out.write (353);//Output ' a ' System.out.flush (); OUT.PRINTLN (345);//convert parameter to string output//previous sentence is equivalent to Out.write (string.valueof (i))//※ in summary, the byte data is output by write () in PrintStream
and prints only one byte at a time, and print () outputs a representation of the value of the data, which is converted to string output. This is the type of out object in the JSP.
To output byte data such as a picture declaration in binary format, you must use write (), while the output page data (character) will be in print () or println ()}}
//Demo automatic refresh of the PrintWriter class
Package io.print;
Import java.io.IOException;
Import Java.io.PrintWriter; /** * * * * @author Chen Haoxiang * * * 2016-4-24//demo PrintWriter class automatic refresh feature public class PrintStreamDemo2 {public static void
Main (string[] args) {demo1 ();
try {Demo2 ();
catch (IOException e) {e.printstacktrace ();
}} private static void Demo1 () {//default PrintWriter out =new printwriter (System.out) not automatically refreshed; Out.print ("Hello World");/does not automatically refresh out.println ("Hello World");//does not automatically refresh Out.flush ()//Manually Refresh} p rivate static void Demo2 () throws IOException {//Set automatic refresh PrintWriter out = new PrintWriter (system.out,tru
e); Out.print ("Hello World");//does not automatically refresh out.println ("Hello World"),//it will----because Out.flush () Out.print () is called Inside the println () "Hello3 \ n");//Will not Out.print ("Hello3 \ r \ n");//Will not out.printf ("%s", "Hello4");//* In summary: * a Utoflush-boolean variable; if the TruE, the println, printf, or Format method refreshes the output buffer.
*---is actually because these methods have helped us call Out.flush ().
*/
}
}
other streams in the IO package The following three are all memory arrays:
★ Byte Array Stream
Bytearrayinputstream and Bytearrayoutputstream
★ Character Array Stream
CharArrayReader and Chararraywriter
★ String Stream
StringReader and StringWriter
1, used to manipulate the flow of byte array objects, in fact, they are the corresponding device for the memory stream object.
2, the shutdown of the stream is not valid because the system resource has not been invoked.
3, according to the flow of reading and writing ideas to manipulate the elements of the array. ★ Sequence Flow
Sequenceinputstream -Merging multiple streams
logical concatenation of multiple streams (merging into a stream, easy to operate because multiple sources become a source) io Flow Knowledge Point Summary
The stream is used to process data.
When working with data, be sure to identify the data source and data destination (data exchange) first.
The data source can be a file, keyboard, or other stream.
Data destinations can be files, monitors, or other streams.
Flow is only to help the data transmission, and the transmission of data processing, such as filtering processing, conversion processing. ★io Flow System
Key points to use: Look at the top layer (parent generic function), using the underlying (subclass specific object). naming laws:
The suffix of each subclass is the name of the parent class of the owning system, and it is easy to distinguish between the systems in which they belong.
And each subclass prefix name is the function embodiment of the subclass object.
Mastering the key points and rules of Io flow system, it is much easier to design and find the corresponding classes when developing.