Escolar Documentos
Profissional Documentos
Cultura Documentos
N i dung
Lu ng nh p xu t l g? Cc lo i lu ng Phn c p l p lu ng Dng Stream i u khi n lu ng nh p xu t. Byte streams Character streams Buffered streams Standard I/O streams Data streams Object streams L p File
2
Khi ni m lu ng?
Lu ng l m t dng ch y c a d li u c g n v i cc thi t b vo ra. Hai lo i lu ng:
Lu ng nh p: G n v i cc thi t b nh p nh bn phm, my scan, file... Lu ng xu t: G n v i cc thi t b xu t nh mn hnh, my in, file...
Vi c x l vo ra thng qua lu ng gip cho l p trnh vin khng ph i quan tm n b n ch t c a thi t b vo ra.
3
Input Stream
Chng trnh s d ng input stream c d li u t ngu n.
Output Stream
Chng trnh s d ng output stream ghi d li u xu ng ch.
Cc lo i lu ng
Character and Byte Streams
Character vs. Byte
Character streams
Cho cc k t Unicode Nh ng l p g c cho character stream: L p Reader L p Writer C 2 l p l tr u t ng (Abstract)
7
Buffered InputStream
LineNumber InputStream
CharArray Reader
InputStream Reader
OutputStream Writer
LineNumberReader
FileReader PushbackReader
Print Stream
11
Abstract Classes
InputStream & OutputStream Reader & Writer
12
c m t byte k ti p c a d li u t lu ng.
public int read(byte[] bBuf) throws IOException
nh d u v tr hi n hnh c a stream. Sau khi nh d u, g i reset() s nh l i v tr c a lu ng n i m ny. Khng ph i t t c lu ng byte input h tr cho thao tc ny.
public int markSupported()
ByteArrayInputStream
Th c thi m t buffer m n ch a cc byte, m n c th c c t ngu n.
PipedInputStream
Nn c lin k t v i PipedOutputStream. Nh ng lu ng ny c s d ng b i 2 lu ng. Trong m t ci l c d li u t ngu n trong khi nh ng ci khc th ghi xu ng PipedOutputStream tng ng.
14
FilterInputStream
For reading filtered byte streams, which may transform the basic source data along the way and provide additional functionalities.
ObjectInputStream
Used for object serialization. Deserializes objects and previuosly written using an ObjectOutputStream.
DataInputStream
A subclass of FilterInputStream that lets an application read Java primitive data from underlying inputstream in a machine-independent way.
LineNumberInputStream
A subclass of FilterInputStream that allows tracking of the current line number.
PushbackInputStream
A subclass of FilterInputStream class that allows bytes to be pushed back or unread into the stream
15
L p tr u t ng OutputStream
public void write(int b) throws IOException Ghi gi tr b xc nh theo d ng byte xu ng output stream public void write(byte[] b) throws IOException Lu n i dung c a m ng byte b xu ng lu ng public void write(byte[] b, int off, int len) throws IOException Lu len byte c a m ng byte b xu ng lu ng, b t u t v tr off c a m ng public void close() throws IOException ng ngu n. G i nh ng phng th c khc lin quan n ngu n ny sau khi g i close se gy ra l i IOException. public void flush() throws IOException flushes the stream.(v d : Nh ng byte c lu trong buffer ngay l p t c c ghi xu ng ch)
16
ByteArrayOutputStream
Implements a buffer that contains bytes, which may be written to the stream
PipedOutputStream
Should be connected to a PipedInputStream. These streams are typically used by two threads wherein one of these threads writes data to this stream while the other thread reads from the corresponding PipeInputStream.
17
FilterOutputStream
For writing filtered byte streams, which may transform the basic source of data along the way and provide additional functionalities.
ObjectOutputStream
Used for object serialization. Serializes object and primitve data to OuputStream.
DataOutputStream
A subclass of FilterOutputStream that lets an application write Java primitive data to underlying output stream in machine-independent way.
PrintStream
A subclass of FilteOutputStream that provides capability for printing representations of various data values conveniently.
18
public abstract int read(char[] cbuf, int off, int len) throws IOException
c len k t v lu chng vo tron m ng cbuf, b t u t i v tr off c a m ng
CharArrayReader
Th c thi m t vng m k t c th c c
StringReader
Cho vi c c t ngu n string
PipedReader
Dng theo c p (tng ng v i PipedWriter) b ng 2 lu ng m chng c th lin l c v i nhau.M t trong nh ng ci lu ng c cc k t .
20
FilterReader
For reading filtered character streams
InputStreamReader
Converts read bytes to characters
LineNumberReader
A subclass of BufferedReader class that able to keep track of line numbers.
PushbackReader
A subclass of the FilterReader class that allows character to pushed back or unread into the stream.
21
CharArrayWriter
Implements a character buffer that can be written to
StringWriter
For writing to a string source
PipedWriter
Used in pairs(with corresponding with PipedReader) by two threads that want to communicate. One of these threads writes characters to this stream.
23
FilterWriter
For writing filtered character streams.
OutputStreamWriter
Encodes characters written to it into bytes.
PrintWriter
Prints formatted representations of objects to a text-output stream.
24
i u khi n lu ng nh p xu t(I/O)
T o i t ng lu ng v lin k t n v i d li u ngu n(data-destination) a ra i t ng lu ng v i ch c nng mong mu n thng qua chu i lu ng (Give the stream object the desired functionality through stream chaining) ng lu ng
25
Byte Stream
Chng trnh s d ng lu ng byte th c hi n nh p xu t nh ng byte 8-bit T t c c l p lu ng byte c k th a t InputStream v OutputStream C nhi u l p lu ng byte
FileInputStream v FileOutputStream
b = read()
write(b)
OutputStream
29
Character Stream
Java platform lu tr nh ng gi tr k t theo d ng Unicode T t c cc l p character stream c k th a t Reader v Writer C cc l p character stream : FileReader v FileWriter.
30
Buffered Streams
M t I/O khng c b m c ngha l m i yu c u c ho c ghi c x l tr c ti p b i OS
i u ny lm chng trnh km hi u qu , v m i yu c u th ng ph i truy xu t a, ho t ng m ng, ho c m t v thao tc kh m n tng i t n th i gian.
T o Buffered Stream?
M t chng trnh c th chuy n m t stream khng c b m thnh stream c b m b ng cch s d ng v bao.
M t unbuffered stream object c chuy n qua hm d ng cho m t l p stream c b m
V d :
inputStream = new BufferedReader(new FileReader("characterinput.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
34
35
Data Streams
Data stream h tr nh p xu t nh phn c a cc lo i d li u nguyn thu (boolean, char, byte, short, int, long, float, and double) cng nh l String T t c data stream th c thi c DataInput cng nh DataOutput interface DataInputStream and DataOutputStream th c thi nh ng phng th c c a nh ng interface ny
37
DataOutputStream
DataOutputStream c th ch c t o nh l m v b c cho i t ng byte stream
out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(dataFile))); for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeInt(units[i]); out.writeUTF(descs[i]); }
38
DataInputStream
Gi ng nh DataOutputStream, DataInputStream ph i c xy d ng nh l m t v b c cho i t ng byte stream i u ki n End-of-file c d b i bi t l EOFException, thay v ki m tra tr v gi tr h t file. in = new DataInputStream(new BufferedInputStream( new FileInputStream(dataFile))); try{ double price = in.readDouble(); int unit = in.readInt(); String desc = in.readUTF(); } catch (EOFException e){ }
39
Object Streams
Object streams support I/O of objects
Like Data streams support I/O of primitive data types The object has to be Serializable type
40
If readObject is to reconstitute an object from a stream, it has to be able to reconstitute all of the objects the original object referred to.
These additional objects might have their own references, and so on.
41
WriteObject
The writeObject traverses the entire web of object references and writes all objects in that web onto the stream A single invocation of writeObject can cause a large number of objects to be written to the stream.
42
43
44
45
46
49