posts - 17,  comments - 12,  trackbacks - 0

           

          一、IO流的三種分類方式

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

              2.按流的數(shù)據(jù)單位不同分為:字節(jié)流和字符流

              3.按流的功能不同分為:節(jié)點(diǎn)流和處理流

              二、IO流的四大抽象類:

              字符流:Reader Writer

              字節(jié)流:InputStream(讀數(shù)據(jù))

              OutputStream(寫數(shù)據(jù))

              三、InputStream的基本方法

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

              void close() throws IOException 關(guān)閉流釋放內(nèi)存資源

              long skip(long n) throws IOException 跳過n個(gè)字節(jié)不讀

              四、OutputStream的基本方法

              void write(int b) throws IOException 向輸出流寫入一個(gè)字節(jié)數(shù)據(jù)

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

              五、Writer的基本方法

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

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

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

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

              void flush() throws IOException

              將輸出流中緩沖的數(shù)據(jù)全部寫出到目的地

              六、Reader的基本方法

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

              七、節(jié)點(diǎn)流類型

              八、訪問文件之FileInputStream和FileOutputStream繼承基類用于向文件中輸入輸出字節(jié)

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

          十、緩沖流:緩沖流要套接在相應(yīng)的節(jié)點(diǎn)流之上,提高了讀寫的效率。

              此處理流的構(gòu)造方法都得傳相對(duì)應(yīng)的基類類型

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

              BufferedWriter:提供了newLine用于寫入一個(gè)行分隔符也就是換行

              BufferedInputStream 沒多大用處

              BufferedOutputStream 沒多大用處

              十一、轉(zhuǎn)換流:主要作用將字節(jié)流轉(zhuǎn)換成字符流。用處較大!

              轉(zhuǎn)換流在構(gòu)造時(shí)可以指定其編碼集合

              InputStreamReader需要和InputStream套接

              OutputStreamWriter需要和OutputStream套接

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

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

              十二、數(shù)據(jù)流與字節(jié)數(shù)組流:

              數(shù)據(jù)流主要為實(shí)現(xiàn)可以存取Java原始數(shù)據(jù)類型如long,boolean

              數(shù)據(jù)流是字節(jié)流

              DataInputStream需要和InputStream套接

              DataOutputStream需要和OutputStream套接

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

              readUTF():網(wǎng)絡(luò)傳輸常用方法 讀一個(gè)Unicode字符串

              DataOutputStream方法與DataInputStream基本對(duì)應(yīng)為寫的方法

              //此構(gòu)造函數(shù)等于已可以往一個(gè)字節(jié)數(shù)組里輸入內(nèi)容

              ByteArrayOutputStream baos = new ByteArrayOutputStream ();

              //此方法為獲取一個(gè)字節(jié)數(shù)組方法返回字節(jié)數(shù)組

              baos.toByteArray();

              //此方法獲取字節(jié)數(shù)組占了多少字節(jié)

              new ByteArrayInputStream(一個(gè)字節(jié)數(shù)組)。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分別針對(duì)字符字節(jié)

              兩個(gè)類提供了重載的Print和Println方法用于多種數(shù)據(jù)類型的輸出

              PrintWriter和PrintStream的輸出操作不會(huì)拋出異常

              PrintWriter和PrintStream有自動(dòng)flush功能

              ----System.setOut(接收OutputStream類):用于設(shè)置系統(tǒng)默認(rèn)輸出流

              十二、Object流

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

              transient關(guān)鍵字為不序列化此成員變量

              需要序列化的類必須實(shí)現(xiàn)Serializable接口

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

              讀出為Object類型需要強(qiáng)轉(zhuǎn)數(shù)據(jù)類型

           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 閱讀(290) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2010年8月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章檔案

          友情連接

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 唐山市| 招远市| 宜章县| 广水市| 都江堰市| 阳朔县| 连平县| 内乡县| 黄石市| 鱼台县| 衡阳市| 海门市| 汉沽区| 昭平县| 九台市| 方城县| 丰原市| 浦城县| 博湖县| 岢岚县| 浦江县| 阳东县| 定西市| 漳浦县| 广东省| 兴宁市| 千阳县| 青海省| 林口县| 固始县| 安阳县| 孟津县| 宜春市| 襄樊市| 盐城市| 武邑县| 师宗县| 宁强县| 即墨市| 清水河县| 尼木县|