IO中的位元組流練習

來源:互聯網
上載者:User

1. FileInputStream  FileOutputStream通過位元組流來讀寫檔案[java]  public static void main(String[] args) throws Exception {      //將檔案裡寫資料      File f = new File("d:\\dmeo.txt");      FileOutputStream output = new FileOutputStream(f);      String s = "I Am Learning Java , 我在學習Java";      byte[] b = s.getBytes(); //將String變為byte數組      //output.write(b);write可以接收byte數組      for (int i = 0; i < b.length; i++) {          output.write(b[i]);      }      output.flush();      output.close();      //讀取檔案裡資料      FileInputStream input = new FileInputStream(f);      //開闢一個檔案大小的數組空間      byte[] in = new byte[(int) f.length()];      //將讀取來的位元組放入數組中      for (int i = 0; i < in.length; i++) {          in[i] = (byte) input.read();       }      System.out.print(new String(in));      input.close();  }   2. BufferedInputStream BufferedOutPutStream 帶有緩衝的讀寫位元組流[java]  public static void main(String[] args) throws Exception {  //將檔案裡寫資料  File f = new File("d:\\dmeo.txt");  BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(f));  String s = "I Am Learning Java , 我在學習Java";  byte[] b = s.getBytes();  output.write(b); //預設會有512位元組的緩衝,當緩衝存滿時一次性向檔案中寫入  output.flush();   output.close();    BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));  byte[] in = new byte[(int)f.length()];      for (int i = 0; i < in.length; i++) {          in[i] = (byte)input.read();      }  System.out.println(new String(in));  input.close();  }   2. DataInputStream DataOutputStream 可以讀寫基礎資料型別 (Elementary Data Type)的位元組流[java]  import java.io.*;  //一個簡單的人員類,只有姓名和年齡  public class Person {      private String name;      private int age;            public Person(String name ,int age){          this.name = name;          this.age = age;      }            public String getName(){          return this.name;      }      public int getAge() {          return this.age;      }                  public static void main(String[] args) {          //構造對象數組          Person[] p1 = {new Person("Ryan",20),new Person("Tom",30),new Person("Jerry",15)};          File f = new File("d:\\demo\\demo.txt");          try {              DataOutputStream output =  new DataOutputStream(new FileOutputStream(f));              //寫入資料              for (int i = 0; i < p1.length; i++) {                  output.writeUTF(p1[i].getName()); //以UTF編碼寫入姓名                  output.writeInt(p1[i].getAge()); //以Int型寫入年齡              }              output.flush();              output.close();          } catch (Exception e) {              e.printStackTrace();          }             Person[] p2 = new Person[p1.length];          try {              //讀出資料              DataInputStream input = new DataInputStream(new FileInputStream(f));              for (int i = 0; i < p1.length; i++) {                  String name =  input.readUTF(); //讀出姓名                  int age  = input.readInt(); //讀出年齡                  //重新構造person對象                  p2[i] = new Person(name,age);                         }                  input.close();          } catch (Exception e) {              e.printStackTrace();          }          //檢查是否還原正確          for (int i = 0; i < p2.length; i++) {              System.out.println("姓名:" + p2[i].getName() + " ;年齡:" + p2[i].getAge());          }            }  }<strong>  </strong>   2. PrintStream  將記憶體中的資料經過相應的轉換後再輸出[java]  public static void main(String[] args){      File f = new File("d:\\dmeo.txt");      String studentName = "TOM";      int age = 20;      double totalScore = 600.0f;      PrintStream ps = null;      try {          ps = new PrintStream(new FileOutputStream(f));      } catch (FileNotFoundException e) {          e.printStackTrace();      }      //格式化後輸出      ps.printf("姓名:%s , 年齡:%d , 總分:%3.1f",studentName,age,totalScore);  }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.