標籤:
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操作