java IO(四):鍵盤錄入

來源:互聯網
上載者:User

標籤:different   log   pos   div   init   render   links   iter   video   

要讀取鍵盤輸入的資料,需要使用輸入資料流,可以是位元組輸入資料流,也可以是位元組輸入資料流轉換後的字元輸入資料流。

關於鍵盤輸入,有幾點注意的是:
(1).鍵盤輸入資料流為System.in,其返回的是InputStream類型,即位元組流。
(2).位元組流讀取鍵盤的輸入時,需要考慮斷行符號符(\r:13)、分行符號(\n:10)。
(3).按行讀取鍵盤輸入時,需要指定輸入終止符,否則輸入將被當作here String文本,而非document。
(4).System.in位元組流預設一直處於開啟狀態,可不用對其close(),但如果close()了,後續將不能使用in流。

例如,以位元組輸入資料流讀取輸入,此時只能輸入ascii碼中的符號。如果需要讀取中文字元,使用位元組流則需要加很多額外的代碼來保證中文字元的位元組不會被切分讀取。

import java.io.*;public class KeyB {    public static void main(String[] args) throws IOException {        InputStream in = System.in;        int ch = in.read();        System.out.println(ch);        int ch1 = in.read();        System.out.println(ch1);        int ch2 = in.read();        System.out.println(ch2);        int ch3 = in.read();        System.out.println(ch3);        int ch4 = in.read();       System.out.println(ch4);    }}

以上樣本中,將讀取鍵盤上輸入的5個位元組,例如輸入"ab"斷行符號後,將輸出"ab\r\n"的ascii碼(97-98-13-10),並繼續等待輸入下一個位元組才退出。

要想一次性讀取一行,可以將它們讀取到位元組數組中,並以斷行符號符、分行符號判斷一行讀取完畢,再將位元組數群組轉換為字串輸出。雖然用位元組流能夠完成這樣的操作,但顯然,如果使用字元流,則更輕鬆。

import java.io.*;public class KeyB {    public static void main(String[] args) throws IOException {        //InputStream in = System.in   //鍵盤輸入位元組流        //InputStreamReader isr = new InputStreamReader(in);   //橋樑,將位元組流轉換為字元流        //BufferedReader bufr = new BufferedReader(isr);       //以BufferReader的readLine()讀取行        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));        String line = null;        //line = bufr.readLine();System.out.println(line);   //輸入一行立即退出        while((line = bufr.readLine())!=null) {      //迴圈輸入多行,直到輸入終止符時才退出            if (line.equals("quit")) {   //定義鍵盤輸入終止符quit                break;            }            System.out.println(line);        }    }}

注意,BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));幾乎是涉及到鍵盤錄入時的一條固定代碼,屬於可以直接背下來的。

以下是一個樣本,需求是將鍵盤輸入的結果儲存到檔案d:\myjava\a.txt中。

import java.io.*;public class Key2File {    public static void main(String[] args) throws IOException {        //1.源:鍵盤輸入,目標:檔案        //2.輸入中可能包含中文字元,所以採用字元流,輸出也採用字元流        //3.需要使用Buffered流來提高效率並使用其提供的行操作方法        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));        BufferedWriter bufw = new BufferedWriter(new FileWriter("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();    }}

註:若您覺得這篇文章還不錯請點擊右下角推薦,您的支援能激發作者更大的寫作熱情,非常感謝!

java IO(四):鍵盤錄入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.