Transcript Java IO

I/O
 java.io套件
 Stream
– 串流:資料由一端接連不斷地傳向另一端
– Source Stream
– Sink Stream
 以byte為單位的資料流
– 8bits
– InputStream
– OutputStream
 以character為單位的資料流
– 16bits(Unicode)
– Reader
– Writer
Stream Classes
Source
Sink
Byte Stream
Character Stream
InputStream
Reader
OutputStream
Writer
Node
 節點(Node):
– 資料讀取的來源或寫入的目的地
Node Stream 類別
種類\派系
位元組
FileInputStream
檔案(File)
FileOutputStream
ByteArrayInputStream
記憶體(陣列)
ByteArrayOutputStream
StringBufferInputStream
記憶體(字串)
管線(Pipe)
PipedInputStream
PipedOutputStream
字元
FileReader
FileWriter
CharArrayReader
CharArrayWriter
StringReader
StringWriter
PipedReader
PipedWriter
讀取及寫入資料演算法
讀取
寫入
開啟資料流
開啟資料流
while (還有資料) { while (還有資料){
讀取資料
寫入資料
}
}
關閉資料流
關閉資料流
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(String str) throws IOException
public void write(String str, int off, int len )
throws IOException
public abstract void flush() throws IOException
public abstract void close() throws IOException
Reader vs. InputStream
Writer vs. OutputStream
FIleInputStream類別
 繼承關係
java.lang.Object
java.io.InputStream
java.io.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
FileReader類別
 繼承關係
java.lang.Object
java.io.Reader
java.io.InputStreamReader
java.io.FileReader
 建構子
public FileReader(String name) throws
FileNotFoundException
public FileReader(File file) throws
FileNotFoundException
FileOutputStream類別
 繼承關係
java.lang.Object
java.io.OutputStream
 建構子
java.io.fileOutputStream
public FileOutputStream(String name,
boolean append) throws
FileNotFoundException
public FileOutputStream(File file,
boolean append) throws
FileNotFoundException
FileWriter類別
 繼承關係
java.lang.Object
java.io.Reader
java.io.OutputStreamWriter
java.io.FileWriter
 建構子
public FileWriter(String fileName,
boolean append) throws IOException
public FileWriter(File file, boolean append)
throws IOException
資料串流連結
 Filter:
– 將資料流連結至特殊的串流,以便使用特定的
方法來存取
Data Source
資料輸入串流 資料輸入Filter
Program
Data Source
資料輸出串流 資料輸出Filter
Program
Filter Stream類別
種類\派系
位元組
BufferedInputStream
暫存(Buffer)
BufferedOutputStream
字元與位元
組轉換
物件序列化
ObjectInputStream
ObjectOutputStream
特定資料型
態存取
DataInputStream
DataOutputStream
字元
BufferedReader
BufferedWriter
InputStreamReader
OutputStreamWriter
種類\派系
位元組
字元
計數
LineNumberInputStream LineNumberReader
重複
PushbackInputStream
PushbackReader
列印
PrintStream
PrintWriter
物件序列化(Serialization)
 Serialization
– 直接把物件作輸出入的處理
– 物件內容輸出到file
 物件序列化步驟
– implements Serializable
– ObjectOutputStream
– ObjectInputStream
Serializable
 要輸出的物件類別需實作Serializable
– 標記介面 Maker interface
只有介面宣告,內部完全沒有任何常數或方法
宣告
 transient
– 某個屬性不想被序列化,加上transient修飾子
ObjectOutputStream
 繼承關係
java.lang.Object
java.io.OutputStream
java.io.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
ObjectInputStream
 繼承關係
java.lang.Object
java.io.InputStream
java.io.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