InputStream
此抽象類是表示字節輸入流的所有類的超類。需要定義 InputStream 的子類的應用程序必須始終提供返回下一個輸入字節的方法。
int available()
返回此輸入流方法的下一個調用方可以不受阻塞地從此輸入流讀取(或跳過)的字節數。
void close()
關閉此輸入流並釋放與該流關聯的所有系統資源。
void mark(int readlimit)
在此輸入流中標記當前的位置。
boolean markSupported()
測試此輸入流是否支持 mark 和 reset 方法。
abstract int read()
從輸入流讀取下一個數據字節。
int read(byte[] b)
從輸入流中讀取一定數量的字節並將其存儲在緩衝區數組 b 中。
int read(byte[] b, int off, int len)
將輸入流中最多 len 個數據字節讀入字節數組。
void reset()
將此流重新定位到對此輸入流最後調用 mark 方法時的位置。
long skip(long n)
跳過和放棄此輸入流中的 n 個數據字節。
OutputStream
此抽象類是表示輸出字節流的所有類的超類。輸出流接受輸出字節並將這些字節發送到某個接收器。需要定義OutputStream 子類的應用程序必須始終提供至少一種可寫入一個輸出字節的方法。
void close()
關閉此輸出流並釋放與此流有關的所有系統資源。
void flush()
刷新此輸出流並強制寫出所有緩衝的輸出字節。
void write(byte[] b)
將 b.length 個字節從指定的字節數組寫入此輸出流。
void write(byte[] b, int off, int len)
將指定字節數組中從偏移量 off 開始的 len 個字節寫入此輸出流。
abstract void write(int b)
將指定的字節寫入此輸出流。
進行I/O操作時可能會產生I/O例外,屬於非運行時例外,應該在程序中處理。如:FileNotFoundException, EOFException, IOException等等,下面具體說明操作JAVA字節流的方法。
讀文件:
本例以FileInputStream的read(buffer)方法,每次從源程序文件OpenFile.java中讀取512個字節,存儲在緩衝區buffer中,再將以buffer中的值構造的字符串new String(buffer)顯示在屏幕上。程序如下(本例程序放在包biz.1cn.stream裏面,另外請在根目錄下建立TestFile.txt文件,以便正常運行):
- package biz.1cn.stream;
- import java.io.FileInputStream;
- import java.io.IOException;
- /**
- * @author chenrz(simon)
- * @date 2006-6-29
- * <p>
- * JAVA字節流例子-讀文件(www.1cn.biz)
- * </p>
- */
- public class ReadFile {
- public static void main(String[] args) {
- try {
- // 創建文件輸入流對象
- FileInputStream is = new FileInputStream("TestFile.txt");
- // 設定讀取的字節數
- int n = 512;
- byte buffer[] = new byte[n];
- // 讀取輸入流
- while ((is.read(buffer, 0, n) != -1) && (n > 0)) {
- System.out.print(new String(buffer));
- }
- System.out.println();
- // 關閉輸入流
- is.close();
- } catch (IOException ioe) {
- System.out.println(ioe);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
- package biz.1cn.stream;
- import java.io.FileInputStream;
- import java.io.IOException;
- /**
- * @author chenrz(simon)
- * @date 2006-6-29
- * <p>
- * JAVA字節流例子-讀文件(www.1cn.biz)
- * </p>
- */
- public class ReadFile {
- public static void main(String[] args) {
- try {
- // 創建文件輸入流對象
- FileInputStream is = new FileInputStream("TestFile.txt");
- // 設定讀取的字節數
- int n = 512;
- byte buffer[] = new byte[n];
- // 讀取輸入流
- while ((is.read(buffer, 0, n) != -1) && (n > 0)) {
- System.out.print(new String(buffer));
- }
- System.out.println();
- // 關閉輸入流
- is.close();
- } catch (IOException ioe) {
- System.out.println(ioe);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
寫文件:
本例用System.in.read(buffer)從鍵盤輸入一行字符,存儲在緩衝區buffer中,再以FileOutStream的write(buffer)方法,將buffer中內容寫入文件WriteFile.txt中,程序如下(本例程序放在包biz.1cn.stream裏面,另外運行後會在根目錄下建立WriteFile.txt文件):
- package biz.1cn.stream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * @author chenrz(simon)
- * @date 2006-6-29
- * <p>
- * JAVA字節流例子-寫文件(www.1cn.biz)
- * </p>
- */
- public class WriteFile {
- public static void main(String[] args) {
- try {
- System.out.print("輸入要保存文件的內容:");
- int count, n = 512;
- byte buffer[] = new byte[n];
- // 讀取標準輸入流
- count = System.in.read(buffer);
- // 創建文件輸出流對象
- FileOutputStream os = new FileOutputStream("WriteFile.txt");
- // 寫入輸出流
- os.write(buffer, 0, count);
- // 關閉輸出流
- os.close();
- System.out.println("已保存到WriteFile.txt!");
- } catch (IOException ioe) {
- System.out.println(ioe);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }