流式IO
流(Stream)是字節的源或目的。
兩種基本的流是:輸入流(Input Stream)和輸出流(Output Stream)。可從中讀出一系列字節的對象稱爲輸入流。而能向其中寫入一系列字節的對象稱爲輸出流。
流的分類
節點流:從特定的地方讀寫的流類,例如:磁盤或一塊內存區域。
過濾流:使用節點流作爲輸入或輸出。過濾流是使用一個已經存在的輸入流或輸出流連接創建的。
InputStream
三個基本的讀方法
abstract int read() :讀取一個字節數據,並返回讀到的數據,如果返回-1,表示讀到了輸入流的末尾。
int read(byte[]?b) :將數據讀入一個字節數組,同時返回實際讀取的字節數。如果返回-1,表示讀到了輸入流的末尾。
int read(byte[]?b, int?off, int?len) :將數據讀入一個字節數組,同時返回實際讀取的字節數。如果返回-1,表示讀到了輸入流的末尾。off指定在數組b中存放數據的起始偏移位置;len指定讀取的最大字節數。
其它方法
long skip(long?n) :在輸入流中跳過n個字節,並返回實際跳過的字節數。
int available() :返回在不發生阻塞的情況下,可讀取的字節數。
void close() :關閉輸入流,釋放和這個流相關的系統資源。
void mark(int?readlimit) :在輸入流的當前位置放置一個標記,如果讀取的字節數多於readlimit設置的值,則流忽略這個標記。
void reset() :返回到上一個標記。
boolean markSupported() :測試當前流是否支持mark和reset方法。如果支持,返回true,否則返回false。
java.io包中 InputStream的類層次
OutputStream
三個基本的寫方法
abstract void write(int?b) :往輸出流中寫入一個字節。
void write(byte[]?b) :往輸出流中寫入數組b中的所有字節。
void write(byte[]?b, int?off, int?len) :往輸出流中寫入數組b中從偏移量off開始的len個字節的數據。
其它方法
void flush() :刷新輸出流,強制緩衝區中的輸出字節被寫出。
void close() :關閉輸出流,釋放和這個流相關的系統資源。
java.io包中 OutputStream的類層次
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Demo1:在控制檯讀取鍵盤輸入,然後顯示。Ctrl+C結束運行
-------------------------------------------------------------------------------
public class StreamDemo {
public static void main(String[] args) throws Exception {
int data;
while ((data = System.in.read()) != - 1 ) {
System.out.write(data);
}
}
}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
基本的流類
1、FileInputStream和FileOutputStream
節點流,用於從文件中讀取或往文件中寫入字節流。如果在構造FileOutputStream時,文件已經存在,則覆蓋這個文件。
-------------------------------------------------------------------------------
Demo2:將字符串寫入特定文件,注意write方法只接收字符數組。
-------------------------------------------------------------------------------
public class StreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream( " 1.txt " );
/** *//**/ /*
* 注意:FileOutputStream的write方法接收字符數組,不能接收String字符串,
* 所以要用String的getBytes方法生成一個字符數組
*/
fos.write( " http://www.cnblogs.com " .getBytes());
fos.close();
}
}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
String的構造方法的API:
------------
Java API:
------------
String
public String(byte[] bytes,
int offset,
int length)
構造一個新的 String,方法是使用指定的字符集解碼字節的指定子數組。新的 String 的長度是一個字符集函數,因此不能等於該子數組的長度。
當給定字節在給定字符集中無效的情況下,該構造方法無指定的行爲。當需要進一步控制解碼過程時,應使用 CharsetDecoder 類。
參數:
bytes - 要解碼爲字符的字節
offset - 要解碼的首字節的索引
length - 要解碼的字節數
拋出:
IndexOutOfBoundsException - 如果 offset 和 length 參數索引字符超出 bytes 數組的範圍
從以下版本開始:
JDK1.1
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Demo3:將字符串寫入特定文件,注意write方法只接收字符數組。
然後通過文件輸出流讀取數據,注意使用String特定的構造方法。
-------------------------------------------------------------------------------
public class StreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream( " 1.txt " );
/** *//**/ /*
* 注意:FileOutputStream的write方法接收字符數組,不能接收String字符串,
* 所以要用String的getBytes方法生成一個字符數組
*/
fos.write( " http://www.cnblogs.com " .getBytes());
fos.close();
// 使用String的這個構造方法:
// String(byte[] bytes, int offset, int length)
FileInputStream fis = new FileInputStream( " 1.txt " );
byte [] buf = new byte [ 100 ];
int len = fis.read(buf);
// 使用String的這個構造方法:
// String(byte[] bytes, int offset, int length)
System.out.println( new String(buf, 0 , len));
fis.close(); // 使用完後記得關閉文件流
}
}
-------------------------------------------------------------------------------
運行結果:http://www.cnblogs.com/
-------------------------------------------------------------------------------
2、BufferedInputStream和BufferedOutputStream
過濾流,需要使用已經存在的節點流來構造,提供帶緩衝的讀寫,提高了讀寫的效率。
------------
Java API:
------------
構造方法摘要
BufferedOutputStream(OutputStream out)
創建一個新的緩衝輸出流,以將數據寫入指定的基礎輸出流。
BufferedOutputStream(OutputStream out, int size)
創建一個新的緩衝輸出流,以將具有指定緩衝區大小的數據寫入指定的基礎輸出流。
-------------------------------------------------------------------------------
BufferedOutputStream
public BufferedOutputStream(OutputStream out)
創建一個新的緩衝輸出流,以將數據寫入指定的基礎輸出流。
參數:
out - 基礎輸出流。
BufferedOutputStream
public BufferedOutputStream(OutputStream out,
int size)
創建一個新的緩衝輸出流,以將具有指定緩衝區大小的數據寫入指定的基礎輸出流。
參數:
out - 基礎輸出流。
size - 緩衝區的大小。
拋出:
IllegalArgumentException - 如果 size <= 0
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Demo4:通過一個OutputStream對象來構造一個BufferedOutputStream對象。
而FileOutputStream類是OutputStream類的子類,所以可以用它來構造。
-------------------------------------------------------------------------------
2
3 public class StreamDemo {
4
5 public static void main(String[] args) throws Exception {
6
7 FileOutputStream fos = new FileOutputStream( " 1.txt " );
8 BufferedOutputStream bos = new BufferedOutputStream(fos);
9 bos.write( " http://www.cnblogs.com " .getBytes());
10 }
11
12 }
-------------------------------------------------------------------------------
運行結果:1.txt文件爲空。原因:緩衝區沒有寫滿,程序沒有向文件寫數據。
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
解決方法1:使用flush()方法:
-------------------------------------------------------------------------------
Demo4:使用flush()方法,可以使程序立即向文件寫數據。
-------------------------------------------------------------------------------
2
3 public class StreamDemo {
4
5 public static void main(String[] args) throws Exception {
6
7 FileOutputStream fos = new FileOutputStream( " 1.txt " );
8 BufferedOutputStream bos = new BufferedOutputStream(fos);
9 bos.write( " http://www.cnblogs.com " .getBytes());
10
11 bos.flush();
12 }
13
14 }
-------------------------------------------------------------------------------
運行結果:數據被寫入相應的文件。
-------------------------------------------------------------------------------
解決方法2:使用close()方法:
-------------------------------------------------------------------------------
Demo5:使用close()方法,同樣可以使程序立即向文件寫數據。
-------------------------------------------------------------------------------
2
3 public class StreamDemo {
4
5 public static void main(String[] args) throws Exception {
6
7 FileOutputStream fos = new FileOutputStream( " 1.txt " );
8 BufferedOutputStream bos = new BufferedOutputStream(fos);
9 bos.write( " http://www.cnblogs.com " .getBytes());
10
11 bos.close();
12 }
13
14 }
-------------------------------------------------------------------------------
運行結果:數據被寫入相應的文件。
-------------------------------------------------------------------------------
flush()和close()的區別:
還要使用流對象,還需要寫數據,使用flush(),否則使用close()。
另外,使用close()將關閉自己的流對象,同時會關閉與之相關的流對象,如FileOutputStream流。
-------------------------------------------------------------------------------
Demo6:使用BufferedInputStream流,從文件中讀取數據。
同樣要用InputStream流對象或者其子類的對象來構造。
-------------------------------------------------------------------------------
2
3 public class StreamDemo {
4
5 public static void main(String[] args) throws Exception {
6
7 FileOutputStream fos = new FileOutputStream( " 1.txt " );
8 BufferedOutputStream bos = new BufferedOutputStream(fos);
9 bos.write( " http://www.cnblogs.com " .getBytes());
10
11 bos.close();
12
13 FileInputStream fis = new FileInputStream( " 1.txt " );
14 BufferedInputStream bis = new BufferedInputStream(fis);
15 byte [] buf = new byte [ 100 ];
16 int len = bis.read(buf);
17
18 // 使用String的這個構造方法:
19 // String(byte[] bytes, int offset, int length)
20 System.out.println( new String(buf, 0 , len));
21 bis.close();
22 }
23
24 }