【Java基礎總結】IO流

來源:互聯網
上載者:User

標籤:read   out   reader   java基礎   offset   char   分享   位元組流   span   

位元組流1. InputStream 位元組輸入資料流

代碼示範

 1     InputStream in = System.in; 2      3     System.out.println("int read(byte b) 方法示範"); 4     //int read() 5     int bt = 0; 6     while((bt=in.read())>0){     7         System.out.print(bt+" "); 8         if(bt == 10){    //斷行符號\r(13) 換行\n(10) 9             break;10         }11     }12     System.out.println("\n\rint read(byte[] buffer) 方法示範");13 14     //int read(byte[] buffer)15     int length = 0;16     byte[] buffer = new byte[10];17     while((length=in.read(buffer)) != 0){18         for(int i=0; i<length; i++){19             System.out.print(buffer[i]+" ");20         }21         break;22     }23     24     System.out.println("\n\rint read(byte[] buffer, int offset, int len) 方法示範");25     26     //int read(byte[] buffer, int offset, int len)27     int len = 1024;28     int count = 0;29     byte[] buf = new byte[len];30     while((count=in.read(buf, 0, len))>0){31         for(int i=0; i<count; i++){32             System.out.print(buf[i]+" ");33         }34         break;35     }36     in.close();

 

2. OutputStream 位元組輸出資料流

 

代碼示範

 1     OutputStream out = System.out; 2     //void write(int b) 3     out.write(65);    //字元A 4      5     out.write(13);    //斷行符號 \r 6     out.write(10);    //換行 \n 7      8     //flush() 9     out.flush();10     11     //write(byte[] bytes)12     byte[] bytes = new String("張曉明").getBytes();13     out.write(bytes);14     15     out.write(13);    //斷行符號 \r16     out.write(10);    //換行 \n17     18     //write(byte[] bytes, int offset, int length)19     bytes = new String("zhangxiaoming").getBytes();20     out.write(bytes, 5, 8);21     22     out.close();

 

字元流1. Reader 字元輸入資料流

代碼示範

 1     Reader reader = new InputStreamReader(System.in); 2      3     //int read() 4     System.out.println("int read() 方法示範"); 5     int c; 6     while((c=reader.read()) != 13){ 7         System.out.print((char)c); 8     } 9     reader.read();10     11     //int read(char[] buf)12     System.out.println("\n\rint read(char[] buf) 方法示範");13     int count = 0;14     char[] buf = new char[1024];15     while((count=reader.read(buf)) > 0){16         String str = new String(buf, 0, count);17         if(str.indexOf("stop")>=0) break;18         System.out.print(str);19     }20     21     //int read(char[] buffer, int offset, int len)22     System.out.println("\n\rint read(char[] buffer, int offset, int len) 方法示範");23     int length = 1024;24     char[] buffer = new char[length];25     while((count=reader.read(buffer, 0, length)) > 0){26         String str = new String(buffer, 0, count);27         if(str.indexOf("stop")>=0) break;28         System.out.print(str);29     }

 

2. Writer 字元輸出資料流

 

代碼示範

 1     Writer writer = new OutputStreamWriter(System.out); 2     String str = "中國"; 3      4     //write(String str) 寫入字串 5     writer.write(str); 6      7     //write(int c)    寫入單個字元 8     writer.write(10);    //分行符號 9     10     //write(String str, int offset, int length) 寫入部分字串11     writer.write(str, 0, 1);12     13     writer.write(10);14     15     //write(char[] buf)    寫入字元數組16     char[] chars = str.toCharArray();17     writer.write(chars);18     19     writer.write(10);20     21     //write(char[] buf, int offset, int length) 寫入部分字元數組22     writer.write(chars, 0, 1);23     writer.write(10);24     25     writer.flush();26     27     //append(char c) 追加字元28     writer.append(‘z‘);29     writer.write(10);30     31     String str2 = "中華人民共和國";32     //append(CharSequence csq)33     writer.append(str2);34     writer.write(10);35     36     //append(CharSequence csq, int offset, int length)37     writer.append(str2, 0, 4);38     39     writer.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.