310 likes | 446 Views
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 類別. 建構子
E N D
I/O • java.io套件 • Stream • 串流:資料由一端接連不斷地傳向另一端 • Source Stream • Sink Stream
以byte為單位的資料流 • 8bits • InputStream • OutputStream • 以character為單位的資料流 • 16bits(Unicode) • Reader • Writer
Node • 節點(Node): • 資料讀取的來源或寫入的目的地
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
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
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
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
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
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
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
java.lang.Object java.io.InputStream java.io.fileInputStream FIleInputStream類別 • 繼承關係 • 建構子 public FileInputStream(String name) throws FileNotFoundException public FileInputStream(File file) throws FileNotFoundException
常用方法 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
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
java.lang.Object java.io.OutputStream java.io.fileOutputStream FileOutputStream類別 • 繼承關係 • 建構子 public FileOutputStream(Stringname, booleanappend) throws FileNotFoundException public FileOutputStream(Filefile, booleanappend) throws FileNotFoundException
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
Data Source Program 資料輸入串流 資料輸入Filter Data Source Program 資料輸出串流 資料輸出Filter 資料串流連結 因可能要一次讀一行,而不是一次1byte or 1 char,所以每次讀寫都先放到 filter,再由 pgm 來讀一行,或寫一行到出去,再轉成byte or char寫出. • Filter: • 將資料流連結至特殊的串流,以便使用特定的方法來存取
物件序列化(Serialization) • Serialization • 直接把物件作輸出入的處理 • 物件內容輸出到file • 物件序列化步驟 • implements Serializable • ObjectOutputStream • ObjectInputStream
Serializable • 要輸出的物件類別需實作Serializable • 標記介面 Maker interface 只有介面宣告,內部完全沒有任何常數或方法宣告 • transient • 某個屬性不想被序列化,加上transient修飾子
java.lang.Object java.io.OutputStream java.io.ObjectOutputStream ObjectOutputStream • 繼承關係 • 建構子 public ObjectOutputStream(OutputStream out) throws IOException
常用方法 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
java.lang.Object java.io.InputStream java.io.ObjectInputStream ObjectInputStream • 繼承關係 • 建構子 public ObjectInputStream(InputStream in) throws IOException
常用方法 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