Professional Documents
Culture Documents
Nguyn Vn Khit
Ni dung
n li kin thc
Bit/ byte Abstract class
Gii thiu
Cc lp chnh lin quan n vic x l IO u nm trong gi java.io Cc lp x l IO trong java c chia thnh 2 lai chnh:
Loi x l lin quan n byte v s, thuc h cc lp InputStream v OutputStream. Loi x l lin quan n k t v text, thuc h cc lp Reader v Writer.
Stream
Stream l mt dy tun t cc byte vi chiu di khng xc nh Input Stream l cc stream thc hin vic di chuyn a dy cc byte vo trong chng trnh Java t mt ngun bn ngoi. Output Stream a dy cc byte t chng trnh Java n cc ni bn ngoi
InputStream
InputStream nh ngha cc phng thc sau
int available( ) void close( ) void mark(int numBytes) boolean markSupported( ) int read( ) int read(byte buffer[ ]) int read(byte buffer[ ], int offset, int numBytes) void reset( ) long skip(long numBytes)
InputStream
Mt s lp con tiu biu ca InputStream:
FileInputStream FilterInputStream ByteArrayInputStream StringBufferInputStream SequenceInputStream PipedInputStream
OutputStream
OutputStream nh ngha cc phng thc sau:
void close( ) void flush( ) void write(int b) void write(byte buffer[ ]) void write(byte buffer[ ], int offset,int numBytes)
OutputStream
Mt s lp con ca OutputStream:
FileOutputStream FilterOutputStream ByteArrayOutputStream PipedOutputStream
Cc i tng c sn ca Java
System.in System.out System.err
System.in
Mt InputStream c d liu trc tip t bn phm.
System.out
Ghi d liu ra console L mt PrintStream
K tha OutputStream C thm cc phng thc print v println K t J2SE 1.5, PrintStream c b sung thm 2 phng thc mi l format v printf
System.err
Ghi li ra console
c v ghi file
c v ghi file di dng byte stream c thc hin thng qua 2 lp FileInputStream v FileOutputStream
c file
To i tng FileInputStream, khi to i tng vi tn file cn c Dng cc hm read() c d liu gi close() ng stream li
Ghi file
To i tng FileOutputStream, khi to i tng vi tn file cn ghi. dng cc hm write() ghi d liu dng flush() a d liu cn trong buffer vo file gi close() ng stream li
Cc InputStream khc
ByteArrayInputStream ObjectInputStream PipedInputStream SequenceInputStream StringBufferInputStream
Cc OutputStream khc
ByteArrayOutputStream ObjectOutputStream PipedOutputStream
hm khi to (constructor) ca cc stream cp cao yu cu phi cung cp mt stream khc. Cc stream c cp cao k tha t mt stream cp thp l FilterInputStream Cc stream ghi cp cao k tha t mt stream cp thp l FilterOutputStream
BufferedInputStream
khi to mt BufferedInputStream
public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int size)
BufferedOutputStream
c cc c tnh tng t BufferedInputStream nhng y l mt stream dng ghi.
S dng PrintStream
Dng ghi trc tip text hoc cc kiu d liu c s. Ta c th khi to PrintStream bng cc cch sau:
PrintStream ps=new PrintStream(OutputStream out); PrintStream ps=new PrintStream(OutputStream out, boolean autoflush);
Reader
Reader l mt lp abstract.
abstract void close( ) void mark(int numChars) boolean markSupported( ) int read( ) int read(char buffer[ ]) abstract int read(char buffer[ ], int offset, int numChars) int read(CharBuffer buffer) boolean ready( ) void reset( ) long skip(long numChars)
Reader
CharArrayReader FilterReader InputStreamReader PipedReader StringReader FileReader
Writer
Writer cng l mt lp abstract Cc phng thc Writer nh ngha:
Writer append(char ch) Writer append(CharSequence chars) Writer append(CharSequence chars, int begin, int end) abstract void close( ) abstract void flush( ) void write(int ch) void write(char buffer[ ]) abstract void write(char buffer[ ], int offset, int numChars) void write(String str) void write(String str, int offset, int numChars)
Writer
CharArrayWriter OutputStreamWriter PipedWriter StringWriter PrintWriter FileWriter
S dng FileWriter
writer chuyn bit dng ghi file, khi to, cn a vo tn file mun ghi
FileReader
chuyn bit cho vic c chui d liu, k t Unicode t file Khi khi to i tng FileReader, ta cn cung cp tn file cn c, nu file ny cha tn ti th s sinh ra exception FileNotFoundException.
InputStreamReader v OutputStreamWriter
InputStreamReader l mt reader c d liu di dng byte t input stream, sau , chuyn d liu sang dng character. InputStreamReader rt ph hp trong trng hp ta mun c d liu character t cc ngun (source) ch cung cp thng tin di dng byte, nh System.in
PrintWriter
y l lp c khuyn khch s dng khi cn a d liu Unicode ra ngoi. PrintWriterExample.java
BufferedReader
BufferedReader l b c cp cao, c d liu t mt b c (Reader) khc. Cc phng thc cung cp bi BufferedReader
int read( ) int read(char data[]) int read(char data[], int start, int max) String readLine()