You are on page 1of 40

Java IO

Nguyn Vn Khit

Ni dung
n li kin thc
Bit/ byte Abstract class

Gii thiu Lm vic vi byte stream Lm vic vi character stream

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

S dng cc byte stream


InputStream v OutputStream l cc abstract class.

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

Chuyn hng System.in, System.out, System.err


public static void setIn(InputStream in) public static void setOut(PrintStream out) public static void setErr(PrintStream err)

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

Lm vic vi cc byte stream cp cao


Cc stream cp cao l cc stream cung cp cc phng thc gip ngi dng lm vic tin li hn Cc stream cp cao
s dng cc stream cp thp thc hin vic vic truy xut d liu trc tip bn thn stream thc hin vic bin i v cung cp cc tin ch trn d liu th nhn c t cc stream cp thp

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

c v ghi d liu nh phn


Gii quyt nhu cu c v ghi d liu di dng s nguyn, s thc, S dng lp DataInputStream v DataOutputStream

DataInputStream implement DataInput interface


boolean readBoolean( ) byte readByte( ) char readChar( ) double readDouble( ) float readFloat( ) int readInt( ) long readLong( ) short readShort( ) int readUnsignedByte() int readUnsignedShort() String readUTF()

DataOutputStream implement DataOutput interface


void writeBoolean(boolean val ) void writeByte(int val ) void writeChar(int val ) void writeDouble(double val ) void writeFloat(float val ) void writeInt(int val ) void writeLong(long val ) void writeShort(int val ) void writeUTF(String s)

S dng BufferedInputStream v BufferedOutputStream


BufferedInputStream
override cc phng thc c cung cp trong InputStream c tng phn d liu ln ri ng gi d liu ny vo mt buffer , t c hiu qu hn so vi vic c tng byte ti nhiu thi im c ch c d liu hon ton trong sut i vi lp trnh vin

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);

Tp tin truy cp ngu nhin


RandomAccessFile khng k tha t InputStream, OutputStream implements cc interface DataInput, DataOutput cung cp phng thc gip ngi dng nh v n mt v tr ca con tr file

Cc phng thc ca RandomAccessFile:


void seek(long newPos) boolean readBoolean( ) byte readByte( ) char readChar( ) double readDouble( ) float readFloat( ) int readInt( ) long readLong( ) short readShort( ) void writeBoolean(boolean val ) void writeByte(int val ) void writeChar(int val ) void writeDouble(double val ) void writeFloat(float val ) void writeInt(int val ) void writeLong(long val ) void writeShort(int val )

c d liu s dng cc character stream


x l vic c ghi d liu di dng k t hoc text, ta nn s dng cc lp c ghi da trn k t do Java cung cp. Cc character stream lm vic vi d liu Unicode Cc character stream gm: b c v b ghi.
cc b c u k tha t Reader cc b ghi th k tha t Writer

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()

You might also like