java--IO操作

來源:互聯網
上載者:User

標籤:

java的IO類操作主要包括如下幾類

 1、File類的使用。

 2、位元組操作流:OutputStream、InputStream

 3、字元操作流:Reader、Writer

 4、對象序列化:serializable

InputStream

InputStream讀取流有3個方法,分別為

第一個:abstract int read()    
Reads a single byte from this stream and returns it as an integer in the range from 0 to 255.

一次從流中讀取一個位元組,並以0到255的integer類型代表該位元組。(例如中文的字元,被拆分分兩個位元組,並以integer類型表示,變成兩個數字了)

執行個體:

public class Test {    public static void main(String[] args) {        String string="測試使用";        byte[] bs=string.getBytes();        System.out.print(bs.length + "  ");        for (int i = 0; i < bs.length; i++) {            System.out.print(bs[i]+ "  ");        }        System.out.print(new String(bs));    }}

結果:8  -78  -30  -54  -44  -54  -71  -45  -61  測試使用  

第二個:int read(byte[] buffer)
Equivalent to read(buffer, 0, buffer.length).

相等於read(buffer, 0, buffer.length)

第三個:int read(byte[] buffer, int offset, int length)

Reads at most length bytes from this stream and stores them in the byte array b starting at offset.

從流中讀取length長度的位元組,並放入到從offset位置開始的b數組中。

其中read()方法是一次讀取一個位元組,效率是非常低的。所以最好是使用後面兩個方法。

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.