Java——IO類,字元流讀資料

來源:互聯網
上載者:User

標籤:bre   S3   rtk   empty   tab   XA   throw   xid   CQ   

InputStreamReader讀資料
█  InputStreamReader讀資料方法
     ?public int read();              //讀取單個字元。      ?public int read(char[] cbuf);             //將字元讀入數組。      ?public int read(char[] cbuf,int off ,int len);            //將字元讀入數組中的某一部分。
public static void main(String[] args) throws IOException {
                FileInputStream fis = new FileInputStream("1.txt");                 InputStreamReader isr = new InputStreamReader(fis);   //沒有寫明編碼格式,預設系統編碼(utf-8);
                //方法一:         /*      int ch;                 // while ((ch = (char) isr.read()) != -1) { //這裡不能在這裡強行轉換成 char ,轉換成 char 就永遠不會等於 -1                 while ((ch = isr.read()) != -1) {                         System.out.print((char) ch);                 } */
                //方法二:         /*     char[] charray = new char[10];                 int leng = isr.read(charray);                 System.out.println(charray);*/
//方法三:         /*     char[] charray2 = new char[10];                 int leng2 = isr.read(charray2, 0,charray2.length);                 System.out.println(charray2);*/
                char[] charray2 = new char[10];                 int leng;                 while(  ( leng = isr.read(charray2,0,charray2.length )  ) != -1 ){                         //System.out.print(charray2);      //這裡不能直接輸出數組,因為不能最後一才讀進數組長度個資料,會導致輸出的部分是上次讀到數組裡的                         String charray2Str = new String(charray2,0,leng);                         System.out.print(charray2Str);                 }
                fis.close();                 isr.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.