Java IO (iv): keyboard entry

Source: Internet
Author: User

To read the data entered by the keyboard, the input stream needs to be used, either as a byte input stream or as a character input stream after the converted byte input stream.

For keyboard input, there are a few things to note:
(1). The keyboard input stream is system.in, which returns the InputStream type, which is the byte stream.
(2). When the byte stream reads input from the keyboard, it is necessary to consider the carriage return (\r:13), line feed (\n:10).
(3). When reading keyboard input by row, you need to specify an input terminator, otherwise the input will be treated as here string literal, not document.
(4). The system.in byte stream is always open by default, but it is not close (), but if close () is not followed, the in stream cannot be used.

For example, to read input in a byte input stream, only symbols in ASCII code can be entered at this time. If you need to read Chinese characters, using a byte stream requires a lot of extra code to ensure that the bytes of Chinese characters are not read by the Shard.

Import java.io.*; Public class KeyB{Public   static void main(string[] args) throws IOException{InputStreaminch= System.inch;intCH =inch. read (); System. out. println (CH);intCH1 =inch. read (); System. out. println (CH1);intCH2 =inch. read (); System. out. println (CH2);intCH3 =inch. read (); System. out. println (CH3);intCH4 =inch. read (); System. out. println (CH4); }}

In the example above, the 5 bytes entered on the keyboard will be read, for example, after entering "AB" Enter, the ASCII code for "ab\r\n" (97-98-13-10) will be output, and the next byte of the input continues to wait for it to exit.

To read one row at a time, you can read them into a byte array, determine a row with a carriage return, line break, and then convert the byte array to a string output. Although this can be done with a byte stream, it is obvious that it is easier to use a character stream.

Importjava.io.*; Public  class KeyB {Public   static void main(string[] args) throws IOException {//inputstream in = system.in//keyboard input byte stream        //inputstreamreader ISR = new InputStreamReader (in); Bridge, converting a byte stream to a character stream        //bufferedreader bufr = new BufferedReader (ISR); Read rows with Bufferreader's ReadLine ()BufferedReader BUFR =NewBufferedReader (NewInputStreamReader (system.in)); String line =NULL;//line = Bufr.readline ();   System.out.println (line); Enter a line to exit immediately         while(line = Bufr.readline ())! =NULL) {//Loop input multiple lines until the input terminates characters exit            if(Line.equals ("Quit")) {//define keyboard input Terminator quit                 Break;        } System.out.println (line); }    }}

Note that BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); it is almost a fixed code that involves keyboard entry, which can be directly memorized.

The following is an example where the requirement is to save the results of the keyboard input to the file d:\myjava\a.txt.

Importjava.io.*; Public  class key2file {Public   static void main(string[] args) throws IOException{//1. Source: Keyboard input, target: file        //2. The input may contain Chinese characters, so a character stream is used, and the output also uses a character stream        //3. Need to use a buffered stream to increase efficiency and use the row action methods it providesBufferedReader BUFR =NewBufferedReader (NewInputStreamReader (system.in)); BufferedWriter BUFW =NewBufferedWriter (NewFileWriter ("D:/myjava/a.txt")); String line =NULL; while((Line=bufr.readline ())! =NULL) {if(Line.equals ("Quit")) { Break;            } bufw.write (line);            Bufw.newline ();        Bufw.flush ();    } bufw.close (); }}

Note: If you think this article is not bad please click on the lower right corner of the recommendation, your support can inspire the author more enthusiasm for writing, thank you very much!

Java IO (iv): keyboard entry

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.