[轉載]關於Java IO流學習總結

    一、IO流的三種分類方式

    1.按流的方向分爲:輸入流和輸出流

    2.按流的數據單位不同分爲:字節流和字符流

    3.按流的功能不同分爲:節點流和處理流

    二、IO流的四大抽象類:

    字符流:Reader Writer

    字節流:InputStream(讀數據)

    OutputStream(寫數據)

    三、InputStream的基本方法

    int read() throws IOException 讀取一個字節以整數形式返回,如果返回-1已到輸入流的末尾

    void close() throws IOException 關閉流釋放內存資源

    long skip(long n) throws IOException 跳過n個字節不讀

    四、OutputStream的基本方法

    void write(int b) throws IOException 向輸出流寫入一個字節數據

    void flush() throws IOException 將輸出流中緩衝的數據全部寫出到目的地

    五、Writer的基本方法

    void write(int c) throws IOException 向輸出流寫入一個字符數據

    void write(String str) throws IOException將一個字符串中的字符寫入到輸出流

    void write(String str,int offset,int length)

    將一個字符串從offset開始的length個字符寫入到輸出流

    void flush() throws IOException

    將輸出流中緩衝的數據全部寫出到目的地

    六、Reader的基本方法

    int read() throws IOException 讀取一個字符以整數形式返回,如果返回-1已到輸入流的末尾

    七、節點流類型

    八、訪問文件之FileInputStream和FileOutputStream繼承基類用於向文件中輸入輸出字節

    九、訪問文件之FileReader和FileWriter繼承基類用於向文件中輸入輸出字符

----輸出流在構造函數第二個參數可以設置true意義爲跟在已有文件後進行輸入
----此類流會拋出FileNotFoundException需要對其進行顯示捕捉

 

    十、緩衝流:緩衝流要套接在相應的節點流之上,提高了讀寫的效率。

    此處理流的構造方法都得傳相對應的基類類型

    BufferedReader:提供了readLine方法用於高校讀取一行字符串

    BufferedWriter:提供了newLine用於寫入一個行分隔符也就是換行

    BufferedInputStream 沒多大用處

    BufferedOutputStream 沒多大用處

    十一、轉換流:主要作用將字節流轉換成字符流。用處較大!

    轉換流在構造時可以指定其編碼集合

    InputStreamReader需要和InputStream套接

    OutputStreamWriter需要和OutputStream套接

    例:OutputStreamWriter osw = new OutputStreamWriter (new FileOutputStream(文件路徑);

    方法例:osw.getEncoding(); 獲得流的編碼方式

    十二、數據流與字節數組流:

    數據流主要爲實現可以存取Java原始數據類型如long,boolean

    數據流是字節流

    DataInputStream需要和InputStream套接

    DataOutputStream需要和OutputStream套接

    DataInputStream方法:readBoolean() readInt() read……()……

    readUTF():網絡傳輸常用方法 讀一個Unicode字符串

    DataOutputStream方法與DataInputStream基本對應爲寫的方法

    //此構造函數等於已可以往一個字節數組裏輸入內容

    ByteArrayOutputStream baos = new ByteArrayOutputStream ();

    //此方法爲獲取一個字節數組方法返回字節數組

    baos.toByteArray();

    //此方法獲取字節數組佔了多少字節

    new ByteArrayInputStream(一個字節數組)。available()

 

 1ByteArrayOutputStream baos = 
 2                        new ByteArrayOutputStream(); 
 3    DataOutputStream dos = 
 4                        new DataOutputStream(baos);
 5    try {
 6      dos.writeDouble(Math.random());
 7      dos.writeBoolean(true);
 8      ByteArrayInputStream bais = 
 9          new ByteArrayInputStream(baos.toByteArray());
10      System.out.println(bais.available());
11      DataInputStream dis = new DataInputStream(bais);
12      System.out.println(dis.readDouble());
13      System.out.println(dis.readBoolean());
14      dos.close();  dis.close();
15    }
 catch (IOException e) {
16      e.printStackTrace();
17    }

 

    十二、Print流

    Print流只有輸出流無輸入流,PrintWriter和PrintStream分別針對字符字節

    兩個類提供了重載的Print和Println方法用於多種數據類型的輸出

    PrintWriter和PrintStream的輸出操作不會拋出異常

    PrintWriter和PrintStream有自動flush功能

    ----System.setOut(接收OutputStream類):用於設置系統默認輸出流

    十二、Object流

    等同於c#序列化,用直接將Object寫入或讀出

    transient關鍵字爲不序列化此成員變量

    需要序列化的類必須實現Serializable接口

    主要方法:writeObject(Object); readObject();

    讀出爲Object類型需要強轉數據類型

 

 1 import java.io.*;
 2 
 3 public class TestObjectIO {
 4     public static void main(String args[]) throws Exception {
 5         T t = new T();
 6         t.k = 8;
 7         FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
 8         ObjectOutputStream oos = new ObjectOutputStream(fos);
 9         oos.writeObject(t);
10         oos.flush();
11         oos.close();
12         
13         FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
14         ObjectInputStream ois = new ObjectInputStream(fis);
15         T tReaded = (T)ois.readObject();
16         System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
17         
18     }
19 }
20 
21 class T 
22     implements Serializable
23 {
24     int i = 10;
25     int j = 9;
26     double d = 2.3;
27     transient int k = 15;
28 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章