流是一組有順序的,有起點和終點的字節集合,是對數據傳輸的總稱或抽象。向內存寫入的是輸入流,從內存讀出的是輸出流。java
在 InputStream 類中,方法 read() 提供了三種從流中讀數據的方法:數組
int read():從輸入流中讀一個字節,造成一個0~255之間的整數返回(是一個抽象方法)app
int read(byte b[]):從輸入流中讀取必定數量的字節,並將其存儲在緩衝區數組 b 中。dom
int read(byte b[],int off,int len):從輸入流中讀取長度爲 len 的數據,寫入數組 b 中從索引 off 開始的位置,並返回讀取得字節數。設計
對於這三個方法,若返回-1,代表流結束,不然,返回實際讀取的字符數。code
代碼示例:orm
package ioTest; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Test t = new Test(); System.out.println("請輸入字符:"); t.copy(System.in,System.out); } private void copy(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[3]; //從InputStream讀取兩個字節,存儲到緩衝區數組buf裏 int len = in.read(buf); System.out.println("=="+len); //字節流的結尾標誌是-1 while(len != -1){ //將緩衝區數組buf中偏移量從0開始的len個字節寫入輸出流 out.write(buf, 0, len); len = in.read(buf); System.out.println("--"+len); } } }
java.io 包中所提供的文件操做類包括:對象
一、用於讀寫本地文件系統中的文件:FileInputStream 和 FileOutputStream繼承
二、描述本地文件系統中的文件或目錄:File、FileDescriptor 和 FilenameFilter索引
三、提供對本地文件系統中文件的隨機訪問支持:RandomAccessFile
FileInputStream 類用於打開一個輸入文件,若要打開的文件不存在,則會產生異常 FileNotFoundException,這是一個非運行時異常,必須捕獲或聲明拋棄;
FileOutputStream 類用來打開一個輸出文件,若要打開的文件不存在,則會建立一個新的文件,不然原文件的內容會被新寫入的內容所覆蓋;
在進行文件的讀/寫操做時,會產生非運行時異常 IOException,必須捕獲或聲明拋棄(其餘的輸入/輸出流處理時也一樣須要進行輸入/輸出異常處理)。
構造方法:
//打開一個以 f 描述的文件做爲輸入 FileInputStream(File f) //打開一個文件路徑名爲 name 的文件做爲輸入 FileInputStream(String name) //建立一個以 f 描述的文件做爲輸出 //若是文件存在,則其內容被清空 FileOutputStream(File f) //建立一個文件路徑名爲 name 的文件做爲輸出 //文件若是已經存在,則其內容被清空 FileOutputStream(String name) //建立一個文件路徑名爲 name 的文件做爲輸出 //文件若是已經存在,則在該輸出上輸出的內容被接到原有內容以後 FileOutputStream(String name, boolean append)
代碼示例:
package ioTest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileInputOutTest { /** * @param args */ public static void main(String[] args) { try { File inFile = new File("d:/494389/Desktop/2.txt"); File outFile = new File("3.txt"); FileInputStream fis = new FileInputStream(inFile); FileOutputStream fos = new FileOutputStream(outFile); int b; while((b = fis.read())!= -1){ fos.write(b); } fis.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
類BufferedInputStream和BufferedOutputStream實現了帶緩衝的過濾流,提供緩衝機制,把任意IO流」捆綁「到緩衝流上,可提升IO流的讀取效率。
BufferedInputStream 的數據成員Buf是一個位數組,默認2048字節。BufferedInputStream 會首先儘可能將 buf 填滿。當使用 read ()方法時,先讀取 buf 中的數據,當 buf 中的數據不滿時,BufferedInputStream 纔會再實現給定的 InputStream 對象的 read() 方法,從源中提取數據。
BufferedOutputStream 的數據成員 buf 是一個位數組,默認爲512字節。當使用 write() 方法寫出數據時,會先將數據寫至到buf 中,當 buf 已滿時纔會實現給定的 OutputStream 對象的 write() 方法,將 buf 數據寫至目的地。
構造方法:
//[ ]裏的內容表明選填 BufferedInputStream(InputStream in [, int size]) BufferedOutputStream(OutputStream out [, int size]) BufferedInputStream 緩衝流和文件流相接,代碼示例: FileInputStream in = new FileInputStream("file.txt"); FileOutputStream out = new FileOutputStream("file2.txt"); //設置輸入緩衝區大小爲256字節 BufferedInputStream bin = new BufferedInputStream(in,256) BufferedOutputStream bout = new BufferedOutputStream(out,256) int len; byte bArray[] = new byte[256]; //將緩衝流讀取256個字節,若是長度不滿256,則讀取實際長度,而後存儲到bArray這個字節數組 len = bin.read(bArray); //len 中獲得的是實際讀取的長度,bArray 中獲得的是數據
對於 BufferedOutputStream,只有緩衝區滿時,纔會將數據真正送到輸出流,但可使用 flush() 方法人爲地將還沒有填滿的緩衝區中的數據寫出。
看下面代碼:
public void copy(InputStream in, OutputStream out) throw IOException { out = new BufferedOutputStream(out, 4096); byte[] buf = new byte[4096]; int len = in.read(buf); while (len != -1) { out.write(buf, 0, len); len = in.read(buf); } //最後一次讀取得數據可能不到4096字節 out.flush(); }
接口 DataInput 和 DataOutput,設計了一種較爲高級的數據輸入輸出方式:除了可處理字節和字節數組外,還能夠處理 int、float、boolean等基本數據類型。
數據流類 DataInputStream 和 DataOutputStream 的處理對象除了是字節或字節數組外,還能夠實現對文件的不一樣數據類型的讀寫:
一、分別實現了 DataInput 和 DataOutput 接口
二、在提供字節流的讀寫手段同時,以統一的形式向輸入流中寫入 boolean,int,long,double 等基本數據類型,並能夠再次把 基本數據類型的值讀取回來。
三、提供了字符串讀寫的手段
數據流類能夠鏈接一個已經創建好的數據對象。
FileInputStream fis = new FileInputStream("file1.txt"); FileOutputStream fos = new FileOutputStream("file2.txt"); DataInputStream dis = new DataInputStream(fis); DataOutputStream dos = new DataOutputStream(fos); 代碼示例: import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataStream { public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub //向文件 a.txt 寫入 FileOutputStream fos = new FileOutputStream("a.txt"); DataOutputStream dos = new DataOutputStream(fos); try { dos.writeBoolean(true); dos.writeByte((byte)123); dos.writeChar('J'); dos.writeDouble(3.1415926); dos.writeFloat(2.122f); dos.writeInt(123); } finally { dos.close(); } //從文件 a.txt 讀出 FileInputStream fis = new FileInputStream("a.txt"); DataInputStream dis = new DataInputStream(fis); try { System.out.println("\t" + dis.readBoolean()); System.out.println("\t" + dis.readByte()); System.out.println("\t" + dis.readChar()); System.out.println("\t" + dis.readDouble()); System.out.println("\t" + dis.readFloat()); System.out.println("\t" + dis.readInt()); } finally { dis.close(); } } }
System.in做爲字節輸入流類InputStream的對象實現標準輸入,經過read()方法從鍵盤接受數據。
int read()
int read(byte b[])
int read(byte b[],int offset,int len)
import java.io.IOException; public class StdInput{ public static void main(String[] args) throws IOException { System.out.println("input:"); byte b[]=new byte[512]; int count=System.in.read(b); System.out.println("Output"); for(int i=0;i<count;i++) { System.out.print(b[i]+" "); } System.out.println(); for(int i=0;i<count;i++) { System.out.print((byte)b[i]+" "); } System.out.println("count="+count); } }
輸出結果:
input: abcd Output 97 98 99 100 13 10 97 98 99 100 13 10 count=6
緣由分析:
一、從鍵盤輸入4個字符abcd並按Enter鍵,保存在緩衝區b中的元素個數爲6,Enter佔用最後兩個字節。
二、其read()方法是讀取字節和字節數組,不能直接獲得須要的數據(如整型,浮點型)。使用Scanner類對標準輸入流System.in的數據進行解析,能夠獲得須要的數據。
System.out做爲打印流PrintStream的對象實現標準輸出,其定義了print和println方法,支持將Java的任意基本類型做爲參數。
public void print(int i); public void println(int i); //JDK5.0後的版本對PrintStream類進行了擴充,支持數據的格式化輸出,增長了printf()方法。 public PrintStream printf(String format,Object…args) public PrintStream printf(Locale 1,String format,Object…args)
System.err也是從 PrintStream 中繼承而來,把錯誤信息送到缺省的顯示設備。
爲了支持在內存上的 I/O,java.io 中提供了類:ByteArrayInputStream、ByteArrayOutputStream 和 StringBufferInputStream
一、ByteArrayInputStream 能夠從指定的字節數組中讀取數據。
二、ByteArrayOutputStream 中提供了緩衝區能夠存放數據(緩衝區大小能夠在構造方法中設定,缺省爲 32),能夠用 write() 方法向其中寫入數據,而後用 toByteArray() 方法將緩衝區中的有效字節寫到 字節數組中去。size() 方法能夠知道寫入的字 節數;reset() 能夠丟棄全部內容。
三、StringBufferInputStream 與 ByteArrayInputStream 相相似,不一樣點在於它是從字符緩衝區 StringBuffer 中讀取16位的 Unicode 數據,而不是8位的字節數據(已被 StringReader 取代)。
java.io 中提供了類 SequenceInputStream,使應用程序能夠將幾個輸入流順序鏈接起來。順序輸入流提供了將多個不一樣的輸入流統一爲一個輸入流的功能,這使得程序可能變得更加簡潔。
代碼示例:
FileInputStream f1,f2; String s; f1 = new FileInputStream("file1.txt"); f2 = new FileInputStream("file2.txt"); SequenceInputStream fs = new SequenceInputStream(f1,f2); DataInputeStream ds = new DataInputStream(fs); while((s = ds.readLine()) != null) { System.out.println(s); }