posts - 3, comments - 1, trackbacks - 0, articles - 5

          J2SE IO流API學習筆記

          Posted on 2009-03-03 10:21 鄭舒力 閱讀(920) 評論(0)  編輯  收藏 所屬分類: J2SE 學習筆記

          關于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 }

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 平谷区| 林西县| 克东县| 剑阁县| 杭州市| 武宣县| 繁峙县| 垦利县| 黎平县| 瓦房店市| 金川县| 松桃| 肃宁县| 正镶白旗| 濉溪县| 新建县| 含山县| 灵璧县| 太谷县| 凌源市| 通道| 长寿区| 日照市| 大洼县| 合作市| 南宁市| 汾阳市| 金昌市| 饶河县| 册亨县| 民县| 井陉县| 东阿县| 察雅县| 伊宁县| 莆田市| 鹤山市| 六枝特区| 阿合奇县| 本溪市| 隆林|