標籤: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類,字元流讀資料