Dark Horse Programmer------IO (iii)

Source: Internet
Author: User

Dark Horse Programmer------IO (iii)

1.1 Read keyboard input via byte stream demo.

System.out: corresponding to the standard output device, the console.
System.in: The corresponding standard input device: keyboard.

Code
InputStream in=system.in;
int By=in.read ();
SOP (by);
End entry

In.close ();

1.1.1 Requirements: input data via keypad (example 1):

When a row of data is entered, the row data is printed.
If the input data is over, then stop the input.

Analysis: The keyboard itself is a standard input device. For Java, there are corresponding objects for this input device.

1 Example 1:2 ImportJava.io.*;3 classReadin4 {5      Public Static voidMain (string[] args)throwsIOException6     {7InputStream in =system.in;8StringBuilder SB =NewStringBuilder ();9 Ten          while(true) One         { A             intCH =In.read (); -             if(ch== ' \ R ')) -                 Continue; the             if(ch== ' \ n ') -             { -String s =sb.tostring (); -                 if("Over". Equals (s)) +                      Break; - System.out.println (S.touppercase ()); +Sb.delete (0, Sb.length ()); A             } at             Else -Sb.append ((Char) ch); -  -         } -     } -}

2.1 IO Conversion Stream (example 2)

Read conversion stream: InputStreamReader, byte flow to character bridge, decoding
Write conversion stream: Outputstreamreader, character flow to a byte bridge, coded

1 Example 2:2 ImportJava.io.*;3 4 classTransstreamdemo5 {6      Public Static voidMain (string[] args)throwsIOException7     {8         //Gets the keyboard entry object. 9         //InputStream in = system.in;Ten  One         //Converts a byte stream object into a character stream object, using the transform stream. InputStreamReader, which is a bridge of bytes flowing to a character stream A         //InputStreamReader ISR = new InputStreamReader (in); -  -         //to improve efficiency, the string is efficiently manipulated by buffer technology. Using BufferedReader the  -         //BufferedReader bufr = new BufferedReader (ISR); -  -  +         //The most common wording of the keyboard.  -BufferedReader BUFR = +                 NewBufferedReader (NewInputStreamReader (system.in)); A  at  -  -  -  -          - //OutputStream out = System.out; in //OutputStreamWriter OSW = new OutputStreamWriter (out); - //bufferedwriter BUFW = new BufferedWriter (OSW); to          +BufferedWriter BUFW =NewBufferedWriter (NewOutputStreamWriter (System.out)); -  the  *  $ Panax NotoginsengString line =NULL; -  the          while((Line=bufr.readline ())! =NULL) +         { A             if("Over". Equals (line)) the                  Break; + Bufw.write (Line.touppercase ()); - bufw.newline (); $ Bufw.flush (); $         } -  - bufr.close (); the  -     }Wuyi}

2.2 When do you use a transform stream?
1, the source or the purpose of the device is a byte stream, but the operation is the text data, you can use the transformation as a bridge, improve the convenience of text operations.
2. Once the text of the operation involves a specific encoding table, the conversion flow must be used.

PS: Character stream inheritance system diagram

A schematic diagram of byte stream inheritance system

The Basic Law of operation of 3.1 io flow

The most painful is that there are many flow objects, do not know which one to use.

Through three clear to complete.

1, clear source and purpose.
Source: input stream. InputStream Reader
Purpose: The output stream. OutputStream Writer.
2, whether the manipulated data is plain text.
Yes: Character Stream.
Not: Byte stream.

3, when the system is clear, specify which specific object to use.
To differentiate by device:
SOURCE device: Memory, hard disk. Keyboard
Destination device: Memory, HDD, console.

4, whether additional features are required
Whether you need to be efficient (buffer):
Yes, just add buffer.

Example:

1, demand: Want to put the keyboard input data stored in a file.
Source: Keyboard.
Purpose: File.

============================================

2, requirements: Want to print the data of a file on the console.
Source: File.
Purpose: Console.

============================================

3, store the data in one text file in another file. Copying files

Source: The read stream is used because it is a source. InputStream Reader
is not manipulating the text file.
Is! Then you can choose Reader
So the system is clear.

Next, specify which object in the system to use.
Clear device: Hard drive. Previous file.
The object that can manipulate files in the reader system is FileReader

Whether you need to improve efficiency: YES!. Join the buffer BufferedReader in the reader system.


FileReader FR = new FileReader ("A.txt");
BufferedReader bufr = new BufferedReader (FR);

Purpose: OutputStream Writer
Whether it is plain text.
Is! Writer.
Device: Hard disk, one file.
Writer system can manipulate the object of the file FileWriter.
Whether you need to improve efficiency: YES!. Adding buffer BufferedWriter in writer system

FileWriter FW = new FileWriter ("B.txt");
BufferedWriter BUFW = new BufferedWriter (FW);

============================================

4, Requirements: The keyboard input data saved to a file.
There are both sources and purposes in this demand.
Then separate analysis
Source: InputStream Reader
Isn't it plain text? Is! Reader

Device: Keyboard. The corresponding object is system.in.
Didn't you choose reader? System.in corresponds to a byte stream?
It is convenient to manipulate the text data of the keyboard. It is most convenient to move to a character stream in a string operation.
So since the reader is clear, then convert the system.in into reader.
Using the reader system to convert the stream, InputStreamReader

InputStreamReader ISR = new InputStreamReader (system.in);

Need to be more efficient? Need! BufferedReader
BufferedReader bufr = new BufferedReader (ISR);

Purpose: OutputStream Writer
Is the text stored? Is! Writer.
Device: Hard drive. A file. Use FileWriter.
FileWriter FW = new FileWriter ("C.txt");
Need to be more efficient? Need.
BufferedWriter BUFW = new BufferedWriter (FW);


**************
To extend the data, you want to store the data in a file according to the specified encoding table (UTF-8).

Purpose: OutputStream Writer
Is the text stored? Is! Writer.
Device: Hard drive. A file. Use FileWriter.
However, FileWriter is the default encoding table used. GBK.

However, when storing, you need to add the specified encoding table Utf-8. Instead, the specified encoding table can be specified only by the transform stream.
So the object to use is OutputStreamWriter.
The transform stream object is to receive a byte output stream. The byte output stream of the file can also be manipulated. FileOutputStream

OutputStreamWriter OSW = new OutputStreamWriter (New FileOutputStream ("D.txt"), "UTF-8");

Do you need to be efficient? Need.
BufferedWriter BUFW = new BufferedWriter (OSW);

So, remember. Convert stream what to use. A bridge between a character and a byte, usually, when a character encoding conversion is involved,
You need to use the transform stream.

============================================

Exercise: Print a text data on the console. To complete three explicit (example 3) in the format above

1 Example 3:2 classTransStreamDemo23 {4      Public Static voidMain (string[] args)throwsIOException5     {6System.setin (NewFileInputStream ("Persondemo.java"));7 8System.setout (NewPrintStream ("Zzz.txt"));9 Ten         //The most common wording of the keyboard.  OneBufferedReader BUFR = A                 NewBufferedReader (NewInputStreamReader (system.in)); -  -          theBufferedWriter BUFW =NewBufferedWriter (NewOutputStreamWriter (System.out)); -  -String line =NULL; -  +          while((Line=bufr.readline ())! =NULL) -         { +             if("Over". Equals (line)) A                  Break; at Bufw.write (Line.touppercase ()); - bufw.newline (); - Bufw.flush (); -         } -  - bufr.close (); in  -     } to}

Dark Horse Programmer------IO (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.