1 / 31

I/O

I/O. java.io 套件 Stream 串流 : 資料由一端接連不斷地傳向另一端 Source Stream Sink Stream. 以 byte 為單位的資料流 8bits InputStream OutputStream 以 character 為單位的資料流 16bits(Unicode) Reader Writer. Stream Classes. Node. 節點 (Node): 資料讀取的來源或寫入的目的地. Node Stream 類別. 讀取及寫入資料演算法. InputStream 類別. 建構子

Download Presentation

I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. I/O • java.io套件 • Stream • 串流:資料由一端接連不斷地傳向另一端 • Source Stream • Sink Stream

  2. 以byte為單位的資料流 • 8bits • InputStream • OutputStream • 以character為單位的資料流 • 16bits(Unicode) • Reader • Writer

  3. Stream Classes

  4. Node • 節點(Node): • 資料讀取的來源或寫入的目的地

  5. Node Stream 類別

  6. 讀取及寫入資料演算法

  7. InputStream類別 • 建構子 public InputStream() • 常用方法 public abstract int read() throws IOException public int read(byte[] b) throws IOException public int read(byte[] b, int off, int len) throws IOException

  8. public int available() throws IOException public long skip(long n) throws IOException public void mark(int readlimit) public void reset() throws IOException public boolean markSupported() public void close() throws IOException

  9. OutputStream類別 • 建構子 public OutputStream() • 常用方法 public abstract void write(int b) throws IOException public void write(byte[] b) throws IOException public void write(byte[] b, int off, int len) throws IOException public void flush() throws IOException public void close() throws IOException

  10. Reader類別 • 建構子 protected Reader() • 常用方法 public int read() throws IOException public int read(char[] c) throws IOException public abstract int read(char[] c, int off, int len) throws IOException

  11. public boolean ready() throws IOException public long skip(long n) throws IOException public void mark(int readAheadLimit ) public void reset() throws IOException public boolean markSupported() public abstract void close() throws IOException

  12. Writer類別 • 建構子 protected Writer() • 常用方法 public void write(int c) throws IOException public void write(char[] c) throws IOException public abstract void write(char[] c, int off, int len) throws IOException

  13. public void write(Stringstr) throws IOException public void write(Stringstr, intoff, intlen ) throws IOException public abstract void flush() throws IOException public abstract void close() throws IOException

  14. Reader vs. InputStream

  15. Writer vs. OutputStream

  16. java.lang.Object java.io.InputStream java.io.fileInputStream FIleInputStream類別 • 繼承關係 • 建構子 public FileInputStream(String name) throws FileNotFoundException public FileInputStream(File file) throws FileNotFoundException

  17. 常用方法 public int read() throws IOException public int read(byte[] b) throws IOException public int read(byte[] b, int off, int len) throws IOException public long skip(long n) throws IOException public int available() throws IOException public void close() throws IOException protected void finalize() throws IOException

  18. java.lang.Object java.io.Reader java.io.InputStreamReader java.io.FileReader FileReader類別 若讀的是英文則補成2bytes,若讀的是中文則直接讀兩bytes包成unicode,所以FileReader 跟 io.Reader之間還會有個InputStreamReader • 繼承關係 • 建構子 public FileReader(String name) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException

  19. java.lang.Object java.io.OutputStream java.io.fileOutputStream FileOutputStream類別 • 繼承關係 • 建構子 public FileOutputStream(Stringname, booleanappend) throws FileNotFoundException public FileOutputStream(Filefile, booleanappend) throws FileNotFoundException

  20. java.lang.Object java.io.Reader java.io.OutputStreamWriter java.io.FileWriter FileWriter類別 • 繼承關係 • 建構子 public FileWriter(String fileName, boolean append) throws IOException public FileWriter(File file, boolean append) throws IOException

  21. Data Source Program 資料輸入串流 資料輸入Filter Data Source Program 資料輸出串流 資料輸出Filter 資料串流連結 因可能要一次讀一行,而不是一次1byte or 1 char,所以每次讀寫都先放到 filter,再由 pgm 來讀一行,或寫一行到出去,再轉成byte or char寫出. • Filter: • 將資料流連結至特殊的串流,以便使用特定的方法來存取

  22. Filter Stream類別

  23. 物件序列化(Serialization) • Serialization • 直接把物件作輸出入的處理 • 物件內容輸出到file • 物件序列化步驟 • implements Serializable • ObjectOutputStream • ObjectInputStream

  24. Serializable • 要輸出的物件類別需實作Serializable • 標記介面 Maker interface 只有介面宣告,內部完全沒有任何常數或方法宣告 • transient • 某個屬性不想被序列化,加上transient修飾子

  25. java.lang.Object java.io.OutputStream java.io.ObjectOutputStream ObjectOutputStream • 繼承關係 • 建構子 public ObjectOutputStream(OutputStream out) throws IOException

  26. 常用方法 public final void writeObject(Object obj) throws IOException public void writeBoolean(boolean val) throws IOException public void writeChar(int val) throws IOException public void writeInt(int val) throws IOException public void writeDouble(double val) throws IOException public void writeChars(String str) throws IOException

  27. java.lang.Object java.io.InputStream java.io.ObjectInputStream ObjectInputStream • 繼承關係 • 建構子 public ObjectInputStream(InputStream in) throws IOException

  28. 常用方法 public final Object readObject() throws IOException, ClassNotFoundException public boolean readBoolean() throws IOException public char readChar() throws IOException public int readInt() throws IOException public double readDouble() throws IOException

More Related