Java——IO類,位元組流讀資料

來源:互聯網
上載者:User

標籤:PQ   MF   fixed   image   dig   使用   html   tno   xhtml 1.0   

位元組流讀取資料 █ InputStream
     ?FileInputStream;             
█ FileInputStream的構造方法     ?FileInputStream(File file);                   //構造對象不比要求檔案存在,會自動建立檔案        ?FileInputStream(String name);              
█ FileInputStream的成員方法     ?public int read();               //read 讀完一次會自動位移到下一個位元組,返回一共讀到位元組長度,讀完檔案尾就返回-1;     ?public int read(byte[] b);                  ?int read(byte[] b, int off, int len);                
public class IOTest2 {
public static void main(String[] args) throws IOException {                  FileInputStream fis = new FileInputStream("1.txt");                  //int read = fis.read();        //檔案裡是 d,當時write(100),寫進檔案是 d 讀出來又變為 100                                                                        //read 唯讀最後一個位元組                  /*System.out.println("read(): "+read);    //read(): 100                  char ch = (char)read;                  System.out.println("轉化:"+ch);       //轉化:d   */                         byte[] bs = new byte[10];                 int readLength = fis.read(bs);         //返回讀到的實際位元組長度;read每次讀完一個位元組會自動位移到下一個                 String string = new String(bs);          //這裡構造string,前面開闢多少空間,就算數組裡只有部分有資料,轉換的時候還是會按定義長度轉換,沒資料的當空格;                                                                        //如果前面的是10 ,後面產生的string就會比是6的時候多一些空白,更長一點                 System.out.println("readTobs: "+string+"  readLength: "+readLength);                                 //readTobs: 201703  readLength: 6        //數組長度為6   //readTobs: 201703      readLength: 6   //數組長度為10                 String string1 = new String(bs,0,readLength);     //消除上面存在的多餘空格                 System.out.println("readTobs: "+string1+"  readLength: "+readLength);
int length = fis.read(bs,0,10);    //參數一表示往哪個數組讀,參數二表示從數組的那個位置寫,第三個參數表示數組長度 String string2 = new String(bs,0,length);       //構建字串的時候指定從數組哪裡開始構建,構建多長; System.out.println("readTobs: "+string2+"  readLength: "+length); fis.close();     //釋放資源         } }



public class IOTest3 {
        public static void main(String[] args) throws IOException {                 FileInputStream fis = new FileInputStream("DiguiTest.java"); /*             int read = 0;                 while((read = fis.read()) != -1){           //read 讀到檔案尾會返回 -1                         char ch = (char)read;                         System.out.print(ch);                 }*/

                //一次讀取一個位元組數組                 //使用位元組流不能處理文本中的中文,會出現亂碼;但是構建成String就不會出現亂碼                 byte[] bytes = new byte[100];                 int length = 0;                 while((length = fis.read(bytes)) != -1){                         String string = new String(bytes,0,length);                         System.out.print(string);                 }                 fis.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.