posts - 17,  comments - 12,  trackbacks - 0

           

          一、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繼承基類用于向文件中輸入輸出字符

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

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

              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()

           1 ByteArrayOutputStream 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 }

          posted on 2010-08-04 10:06 asiawang 閱讀(296) 評論(0)  編輯  收藏

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


          網站導航:
           
          <2010年8月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 太保市| 凉山| 新疆| 井研县| 江城| 同德县| 邢台县| 耒阳市| 盱眙县| 乡城县| 兴海县| 会理县| 遵义县| 蓬溪县| 宣城市| 漳浦县| 怀仁县| 南涧| 若尔盖县| 罗田县| 长兴县| 普兰店市| 延庆县| 宾阳县| 九台市| 都匀市| 金坛市| 来凤县| 阳曲县| 莱州市| 弥勒县| 鄂伦春自治旗| 巴彦淖尔市| 惠安县| 拉萨市| 大石桥市| 钟山县| 武汉市| 邛崃市| 达孜县| 岑溪市|