Core Java學習筆記 流與文件

          Posted on 2009-07-19 16:23 eric_xu 閱讀(394) 評論(0)  編輯  收藏 所屬分類: Java

          流與文件

          InputStream

              Abstract int read()

          OutputStream

              Abstract void write(int b)

          輸入和輸出流的層次結構


          DataInputStreamDataOutputStream能夠讀寫所有的基本java類型,InputStreamOutputStream僅能讀寫單個字節和字節數組。ZipOutputStreamZipInputStream讀寫ZIP格式的壓縮文件。

          對于Unicode文本,可以使用ReaderWriter中派生出來的類。

          InputStreamOutputStreamReaderWriter都實現了Closeable接口,OutputStreamWriter實現Flushable接口。Writer實現Appendable接口。

          Appendable append(CharSequence csq) throws IOException;

          Appendable對象后追加csq中所有代碼單元,返回當前對象。

          Appendable append(char c) throws IOException;

           

          FileInputStreamFileOutputStream、能夠把輸入輸出流與磁盤文件關聯起來。只能讀取字節。

           

          DataOutput接口和DataInput接口聲明了所有讀寫Java基本類型的方法。

          RandomAccessFile流類能夠在文件的任何位置查找或寫入數據。RandomAccessFile實現了DataOutput接口和DataInput接口。

          InputStreamReader類可將特殊字符編碼方案的輸入流轉換成Unicode字符,OutputStreamWriter類能將Unicode字符流轉換成特殊字符編碼方案的字節流。

          public InputStreamReader(InputStream in, String charsetName)

           

          Charset cset = Charset.forName("ISO-8859-1");

           

          Set<String, Charset> charsets = Charset.availableCharsets();

          for (String name : charsets.keySet())

             System.out.println(name);

           

          Unicode字符串進行編碼

          String str = . . .;

          ByteBuffer buffer = cset.encode(str);

          byte[] bytes = buffer.array();

          對一個字節序列解碼,需要一個字節緩沖區。

          byte[] bytes = . . .;

          ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length);

          CharBuffer cbuf = cset.decode(bbuf);

          String str = cbuf.toString();

           

          java.nio.charset.Charset 1.4

          ByteBuffer encode(String str)

          encodes the given string into a sequence of bytes.將給定字符串編碼成一個字節序列。

           

          文本輸出PrintWriter以文本格式打印字符串和數值,但沒有定義目的地。一個PrintWriter必須與一個Writer結合使用。

          PrintWriter out = new PrintWriter(new FileWriter("employ.txt"));

          或者

          PrintWriter out = new PrintWriter(new FileOutputStream("employ.txt"));

          寫入PrintWriter,可以使用System.outprintprintln方法。

          PrintWriter out = new PrintWriter(new FileWriter("employee.txt"), true); // autoflush自動刷新

           

          二進制格式寫入數據時,使用DataOutputStream

          文本格式寫入數據時,使用PrintWriter

          文本格式讀取數據時,使用BufferedReader

          BufferedReader in = new BufferedReader(new FileReader("employee.txt"));

          FileReader類已經把字節轉化為Unicode字符。對于其他輸入源,需要使用InputStreamReader,與PrintWriter不同,InputStreamReader沒有包含自動彌補字節與Unicode字符之間差別的方法。

          BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));

          BufferedReader in3 = new BufferedReader(new InputStreamReader(url.openStream()));

           

          String line;
          while ((line = in.readLine()) != null)
          {
             do something with line
          }

           

          Zip文件流讀取

          ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));

          //通過將一個FileInputStream對象傳給ZipInputStream
          ZipEntry entry;
          while ((entry = zin.getNextEntry()) != null)
          {
             analyze entry;
             read the contents of zin;
             zin.closeEntry();
          }
          zin.close();

          或者

          BufferedReader in = new BufferedReader(new InputStreamReader(zin));
          String s;
          while ((s = in.readLine()) != null)
             do something with s;

          Zip文件流寫入

          FileOutputStream fout = new FileOutputStream("test.zip");
          ZipOutputStream zout = new ZipOutputStream(fout);

          //FileOutputStream傳遞給ZipOutputStream
          for all files
          {
             ZipEntry ze = new ZipEntry(filename);
             zout.putNextEntry(ze);
             send data to zout;
             zout.closeEntry();
          }
          zout.close();

           

          StringBuilder

          StringBuilder避免每次重新分配空間,它管理著一個可以根據需求增長或縮短的字符數組char[]

           

          writeFixedStringreadFixedString讀寫指定長度的Unicode字符串。

           

          對象流

          存儲對象

          ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));

          Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
          out.writeObject(harry);

          讀取對象

          ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));

          Employee e1 = (Employee) in.readObject();

          對于任意需要在對象流中存儲和恢復的類,必須實現Serializable接口

          class Employee implements Serializable { . . . }

          對象序列化使用一種特殊的文本格式來存儲對象。

          posts - 37, comments - 5, trackbacks - 0, articles - 0

          Copyright © eric_xu

          主站蜘蛛池模板: 军事| 中西区| 满城县| 清镇市| 卓资县| 黄浦区| 通河县| 恭城| 辽宁省| 厦门市| 高台县| 辽阳县| 姚安县| 无极县| 岳普湖县| 芦溪县| 濮阳市| 芒康县| 闵行区| 潞西市| 光泽县| 太保市| 梓潼县| 绥滨县| 景洪市| 武宁县| 三穗县| 温州市| 平遥县| 清远市| 柳河县| 宁津县| 手游| 昌邑市| 永德县| 博乐市| 托克托县| 调兵山市| 齐齐哈尔市| 南木林县| 白河县|