java的IO流的典型應用

來源:互聯網
上載者:User

儘管庫記憶體在大量IO流類,可通過多種不同的方式組合到一起,但實際上只有幾種方式才會經常用到。然而,必須小心在意才能得到正確的組合。下面這個相當長的例子展示了典型IO配置的建立與使用,可在寫自己的代碼時將其作為一個參考使用。注意每個配置都以一個注釋形式的編號起頭,並提供了適當的解釋資訊。
 

//: IOStreamDemo.java// Typical IO Stream Configurationsimport java.io.*;import com.bruceeckel.tools.*;public class IOStreamDemo {  public static void main(String[] args) {    try {      // 1. Buffered input file      DataInputStream in =        new DataInputStream(          new BufferedInputStream(            new FileInputStream(args[0])));      String s, s2 = new String();      while((s = in.readLine())!= null)        s2 += s + "\n";      in.close();      // 2. Input from memory      StringBufferInputStream in2 =          new StringBufferInputStream(s2);      int c;      while((c = in2.read()) != -1)        System.out.print((char)c);      // 3. Formatted memory input      try {        DataInputStream in3 =          new DataInputStream(            new StringBufferInputStream(s2));        while(true)          System.out.print((char)in3.readByte());      } catch(EOFException e) {        System.out.println(          "End of stream encountered");      }      // 4. Line numbering & file output      try {        LineNumberInputStream li =          new LineNumberInputStream(            new StringBufferInputStream(s2));        DataInputStream in4 =          new DataInputStream(li);        PrintStream out1 =          new PrintStream(            new BufferedOutputStream(              new FileOutputStream(                "IODemo.out")));        while((s = in4.readLine()) != null )          out1.println(            "Line " + li.getLineNumber() + s);        out1.close(); // finalize() not reliable!      } catch(EOFException e) {        System.out.println(          "End of stream encountered");      }      // 5. Storing & recovering data      try {        DataOutputStream out2 =          new DataOutputStream(            new BufferedOutputStream(              new FileOutputStream("Data.txt")));        out2.writeBytes(          "Here's the value of pi: \n");        out2.writeDouble(3.14159);        out2.close();        DataInputStream in5 =          new DataInputStream(            new BufferedInputStream(              new FileInputStream("Data.txt")));        System.out.println(in5.readLine());        System.out.println(in5.readDouble());      } catch(EOFException e) {        System.out.println(          "End of stream encountered");      }      // 6. Reading/writing random access files      RandomAccessFile rf =        new RandomAccessFile("rtest.dat", "rw");      for(int i = 0; i < 10; i++)        rf.writeDouble(i*1.414);      rf.close();      rf =        new RandomAccessFile("rtest.dat", "rw");      rf.seek(5*8);      rf.writeDouble(47.0001);      rf.close();      rf =        new RandomAccessFile("rtest.dat", "r");      for(int i = 0; i < 10; i++)        System.out.println(          "Value " + i + ": " +          rf.readDouble());      rf.close();      // 7. File input shorthand      InFile in6 = new InFile(args[0]);      String s3 = new String();      System.out.println(        "First line in file: " +        in6.readLine());        in6.close();      // 8. Formatted file output shorthand      PrintFile out3 = new PrintFile("Data2.txt");      out3.print("Test of PrintFile");      out3.close();      // 9. Data file output shorthand      OutFile out4 = new OutFile("Data3.txt");      out4.writeBytes("Test of outDataFile\n\r");      out4.writeChars("Test of outDataFile\n\r");      out4.close();    } catch(FileNotFoundException e) {      System.out.println(        "File Not Found:" + args[0]);    } catch(IOException e) {      System.out.println("IO Exception");    }  }} ///:~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.