Lec08 :: class input/output - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Lec08 :: class input/output

Description:

... byte stream reader writer character stream java.io ... (pipe) ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 51
Provided by: thaitv
Category:
Tags: class | input | java | lec08 | output | pipe | stream

less

Transcript and Presenter's Notes

Title: Lec08 :: class input/output


1
Lec08 class input/output
  • ?????????????????????
  • (Input/Output Classes)
  • ??? ?. ??????? ????????
  • http//www.siam2dev.com

2
????????????
  • ?????????????????????????? stream
  • ?????????????????????????????????? java.io
  • ???????????????????????? InputStream ???
    OutputStream
  • ???????????????????????? Reader ??? Writer
  • ???????????????????????????? Reader ??? Writer
    ??? stream
  • ???????????????????? stream ????????

3
????????????
  • ???????????????????????? File
  • ????????????? ???????????????????????????
    RandomAccessFile
  • ?????????????????????????????? Serializable
  • ?????????????????????????????????????????????
    stream

4
Stream
  • ????????????????????????????????? (source)
    ???????????? (sink)
  • stream ????????????????????????
    ??????????????????????????????????????????????????
    ???????????????????????????????
    ???????????????????????????????????????????????
  • source ?????????????????????? stream ????????
    input stream
  • sink ????????????????????? stream ???????? output
    stream

5
Stream
  • source ???? sink ?????????????????????????????
    ???? ???? ??????????? ???? socket ???????
  • ???????????? stream ???????
  • byte stream
  • character stream
  • ????? stream ?????????????????? byte stream
  • reader ??? writer ????????? character stream

6
??????? java.io
  • ??????????????????????????????????????????????
    Java API ????????? java.io ???????????????????????
    4 ???????
  • InputStream
  • ?????????????????????????????????????? stream
    ????????????????????? byte
  • OutputStream
  • ?????????????????????????????????????? stream
    ????????????????????? byte
  • Reader
  • ?????????????????????????????????????? stream
    ????????????????????? char
  • Writer
  • ?????????????????????????????????????? stream
    ????????????????????? char

7
?????????? InputStream
8
?????????? OutputStream
9
?????????? Reader
10
?????????? Writer
11
?????????? Byte Stream
  • ??????????????????????????????????????????????????
    ???????????????????????? byte ????????????????????
    ??? InputStream ??? OutputStream
  • ?????????????????????? abstract
    ??????????????????????????????????????????????????
    ????????????????????????????? subclass
    ??????????????????????????????????????????????????
    ??? byte ?????????????????????????????????????????
    ???
  • FileInputStream ??? FileOutputStream
    ???????????????????????? ????????????????????????
    ????????????????
  • ByteArrayInputStream ??? ByteArrayOutputStream
    ??????????????????????????????????????????????????
    ??? ????????????????????????????? byte

12
???? InputStream
  • ???? InputStream ??????????????????????? stream
    ???????????????????? byte
  • ???? InputStream ???????????????????????????????
    stream ??????????????????? stream
    ?????????????????????????????? stream
    ?????????????????????????????????????????? byte
    ????????????? byte ??????????????
  • int read()
  • int read(byte b)
  • int read(byte b,int offset,int length)

13
???? InputStream
  • ??????????????????
  • void close()
  • int available()
  • void skip(long n)
  • boolean markSupported()
  • void mark(int readlimit)
  • void reset()

14
???? OutputStream
  • ???? OutputStream ???????????????????? stream
    ???????????????????? byte ????????????????????????
    ?? Outputstream ???????????????????????? stream
  • ???? OutputStream ???????????????????????????
    byte ??????????????????? read() ??????
    InputStream ??????????????????? write()
    ??????????????? abstract ???????????????????
  • void write(int c)
  • void write(byte b)
  • void write(byte b,int offset,int length)

15
???? OutputStream
  • ??????????????????
  • void close()
  • void flush()

16
?????????? Character Stream
  • ??????????????????????????????????????????????????
    ????????????????????????? char ??????????????
    Reader ??? Writer
  • ?????????????????????? abstract ????? subclass
    ??????????????????????????????????????????????????
    ?????????????????????????????????????? ????
    ??????????? ??????? ???????

17
???? Reader
  • Reader ???????????????????????????????? stream
    ???????????????????? char
  • Reader ????????????????????????? InputStream
    ?????????????????????????????????????????????????
    argument ????????????????? char
    ?????????????????????
  • int read()
  • int read(char c)
  • int read(char c,int offset,int length)
  • void close()
  • boolean ready()
  • void skip(long n)
  • boolean markSupported()
  • void mark(int readAheadlimit)
  • void reset()

18
???? Writer
  • Writer ????????????????????????????????? stream
    ???????????????????? char
  • Writer ????????????????????????? OutputStream
    ???????????????????????????????????????????
    argument ????????????????? char
    ?????????????????????
  • void write(int c)
  • void write(char c)
  • void write(char c,int offset,int length)
  • void close()
  • void flush()
  • ????????????????????????????????????????? String
  • void write(String s)
  • void write(String s,int offset,int length)

19
?????????? Stream
  • ???????????????????????????????????????????
    stream ???????????? ???? ??????????? ???????
    (pipe)
  • ????????????????? stream ?????????????????????????
    ??????????????? byte
  • ???????????????????????? stream
    ????????????????????????????????????????????????
    String
  • ????????????????? stream ?????????????????????????
    ??? process ???????????????

20
????????????????????? stream ?????
???? Byte Stream Character Stream
File FileInputStream FileOutputStream FileReader FileWriter
Memory Array ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
Memory String - StringReader StringWriter
Pipe PipedInputStream PipedOutputStream PipedReader PipedWriter
21
??????????????? FileCopy.java
import java.io. public class FileCopy
public static void main(String args)
try FileInputStream fin new
FileInputStream(args0) FileOutputStream fout
new FileOutputStream(args1) byte
b new byte100 int i fin.read(b) whil
e (i ! -1) fout.write(b,0,i) i
fin.read(b) fin.close() fout.close()
catch (IOException e)
22
??????????????? FileCopyReader.java
import java.io. public class FileCopyReader
public static void main(String args) try
FileReader fr new FileReader(args0)
BufferedReader br new BufferedReader(fr)
FileWriter fw new FileWriter(args1)
BufferedWriter bw new BufferedWriter(fw)
String line br.readLine() while
(line ! null) bw.write(line,0,line.length())
bw.newLine() line br.readLine()
br.close() bw.close() catch
(IOException e)
23
?????????????????????
  • ??????? FileCopy.java ????????????????????????????
    ???????????????????????????????? ?????? byte
    stream
  • ??????? FileCopyReader.java ??????????????????????
    ???????????????????????????????? FileCopy.java
    ???????? character stream

24
??????????????????????????
  • ?????????????????????????????????????????? stream
    ???????????????????????????????????????? stream
    ??????????????????????????????????????????????????
    ????
  • ??????????????????????????????????????
    FileInputStream ??????????????????????????????????
    ???? byte ????????????????????? DataInputStream
    ?????????????????????????????????????????
  • FileInputStream fin new FileInputStream(test.d
    at)
  • DataInputStream din new DataInputStream(fin)

25
?????????? stream ????????
  • ??????????????????????????? stream
    ?????????????????????????? steam ????????
    (high-level stream) ??????????????????????????????
    ??????????????????????????????????????????????????
    ?????????? byte ??????????????????????????????????
    ?????????????????????????
  • ??????????????????????????????????????????????????
    ????????????????????????????? ??????????? ????
    Pipe ????????? ??????????????????? stream
    ???????????????????????????????????????????????

26
?????????? stream ????????????????
  • DataInputStream ??? DataOutputStream
  • ??????????????????????????????????????????????????
    ??? byte ?????????????????????
  • BufferedInputStream ??? BufferedOutputStream
  • ????????????????????????????????????? byte
    ??????????????????????????????????????????????????
    ?????????????????????????????????????????????????
    ?
  • PrintStream
  • ?????????????????????????????????????? String
    ????????????????????????? byte ?????????????? out
    ??? err ???????????????????? System
    ???????????????????????????????????
  • PushbackInputStream
  • ????????????????????????????? byte
    ??????????????????????? stream ???

27
?????????? stream ????????????????
  • ButteredReader ??? BufferedWriter
  • ???????????????????????????????????????? char
    ???????????????????????????????????????????
  • InputStreamReader ??? OutputStreamWriter
  • ??????????????????????????????????????????????????
    ??? char ?????????????????????
  • PrintWriter
  • ?????????????????????????????????????? String
    ????????????????????????? char
  • PushbackReader
  • ????????????????????????????????? char
    ??????????????????????? stream ???

28
?????????? stream ????????
???? Byte Stream Character Stream
Buffering BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
Filtering FilterInputStream FilterOutputStream FilterReader FilterWriter
Data conversion DataInputStream DataOutputStream -
Printing PrintStream PrintWriter
Peeking ahead PushbackInputStream PushbackReader
29
???????????? Stream ????????
  • ?????????????????? Stream ??????????? ????
  • FileInputStream f new FileInputStream(text.dat
    )
  • BufferedInputStream b new BufferedInputStream(f
    )
  • DataInputStream d new DataInputStream(b)

30
???? DataInputStream
  • ???? DataInputStream ?????????????????????????????
    ????????
  • boolean readBoolean()
  • byte readByte()
  • char readChar()
  • double readDouble()
  • float readFloat()
  • int readInt()
  • long readLong()
  • short readShort()
  • String readUTF()

31
???? DataOutputStream
  • DateOutputStream ???? high-level FilterStream
    ??????????????????????????????????????????
  • void writeBoolean(boolean b)
  • void writeByte(int b)
  • void writeByte(String s)
  • void writeChar(int c)
  • void writeDouble(double d)
  • void writeFloat(float f)
  • void writeInt(int i)
  • void writeLong(long l)
  • void writeShort(int s)
  • void writeUTF(String s)

32
????????????????????? DataOutputStream
import java.io. public class DataWriterDemo
public static void main (String args)
try FileOutputStream
fout DataOutputStream dout fout new
FileOutputStream("data.dat") dout new
DataOutputStream(fout) dout.writeInt(124)
dout.writeDouble(2.45) dout.writeChar('a')
dout.writeUTF("test") dout.close() fout.c
lose() catch (IOException ex)
System.out.println(ex.toString())
33
??????????????? DataInputStream
import java.io. public class DataReaderDemo
public static void main (String args)
try FileInputStream
fin DataInputStream din fin new
FileInputStream("data.dat") din new
DataInputStream(fin) int x
din.readInt() double d din.readDouble()
char ch din.readChar() String line
din.readUTF() System.out.println("x " x
" d " d " ch " ch) System.out.pri
ntln(line)
34
??????????????? DataInputStream
din.close() fin.close() catch
(IOException ex) System.out.println(ex.toStri
ng())
x 124 d 2.45 ch a test
?????????????????????????????
35
???????????????????? byte ??? char
  • ?????????????????? stream ????????????????????
    byte ???????????????? char ?????????
  • ???? InputStreamReader ???????????????????????
    byte ????????????????????????? char
  • ???? OutputStreamWriter ???????????????????????
    char ????????????????????????? byte

36
???????????????
import java.io. public class FileWriter
public static void main(String args)
FileOutputStream fout OutputStreamWriter
oout PrintWriter p String line1 "This is
a test message" String line2 "This is
another line" try fout new
FileOutputStream("data.dat") oout
new OutputStreamWriter(fout) p new
PrintWriter(oout) p.println(line1) p.print
ln(line2)
37
???????????????
p.close() oout.close() fout.close()
catch (IOException ex) System.out.printl
n(ex.toString())
38
???? File
  • ??????????? File ????????????????????????
    java.io ??????????????????????????????????????????
    ???????????????
  • ???? File ????????????????????????????????????????
    ?? ???????????????????????????????????????????
  • ?????????????? File ???????????? constructor
    ?????????????????
  • public File(String name)
  • public File(String dir, String name)
  • public File(File dir, String name)

39
???????????? File
  • ???????????? File ????????????????????????????????
    ????????????????????????
  • boolean exists()
  • boolean isFile()
  • boolean isDirectory()
  • String getName()
  • String getParent()
  • String list()
  • boolean canWrite()
  • boolean mkdir()
  • boolean renameTo(File newName)

40
?????????????????????? File
  • ????????????????????????????? File
    ????????????????????????????????????
  • Constructor ??? FileInputStream/FileOutputStream
    ???? FileReader/FileWriter ?? argument
    ?????????????????????? File
  • ????????
  • File f new File(test.dat)
  • if (f.exists())
  • FileInputStream fir new FileInputStream(f)

41
???? RandomAccessFile
  • ??????????????????????????????????????????????????
    ?? random access ?????????????????????????????????
    ??????????????????????????????????????????????????
    ????????????????????? ????????????????????
    RandomAccessFile ?????????????????????????????????
    ?????????????????????????
  • ???? RandomAccessFile ???? constructor
    ???????????????????
  • public RandomAccessFile(String name,String mode)
  • public RandomAccessFile(File file,String mode)
  • mode ????????????
  • r ??????????????????????????????
  • rw ??????????????????????????????????????

42
???????????? RandomAccessFile
  • ???? RandomAccessFile ????????????????????????????
    ?????????????????????????????????????????????????
    ??????? DataInputStream ??? DataOutputStream
  • ???? RandomAccessFile ????????????????????????????
    ?????????
  • long getFilePointer()
  • long length()
  • void seek(long pos)

43
ObjectStream
  • ??????????????????????????????????????????????????
    ???????
  • ObjectInputStream
  • ObjectOutputStream
  • ???????????????????????????????? stream
    ?????????????????????????????????????? implements
    ??????????? Serializable ????????????????????????
    ???? java.io
  • ???????????????????????????????? implements
    ??????????? Serializable ??? ?????????????????????
    ????????????????????????????? ????????
    ?????????????? Thread ???? FileInputStream

44
ObjectStream
  • ??????????????????????????????????????????????????
    ????????????????????????????????????????????
    stream ???????????????????????????????????
    modifier ????????????????????? transient
    ????????????
  • public class Student implements Serializable
    private String id,name
  • private double gpa
  • private transient String parent
  • ???? ObjectOutputStream ??????????????????????????
    ???????????? stream ?????????? writeObject()??????
    ????????????????
  • ???????? ObjectInputStream ???????????????????????
    ?????????????? stream ?????????? readObject()
    ?????????????????????

45
???????????????
import java.io. public class
ObjectPersistenceDemo public static void
main(String args) Student s new
Student("Somchai", "4514452-2",
3.21) FileOutputStream fout ObjectOutputStre
am oout FileInputStream fin ObjectInputStrea
m oin try fout new
FileOutputStream("data.dat") oout new
ObjectOutputStream(fout) oout.writeObject(s)
System.out.println("Write to file
successfully") oout.close() fout.close()
46
???????????????
fin new FileInputStream("data.dat") oin
new ObjectInputStream(fin) Student s2
(Student) oin.readObject() oin.close() fin
.close() s2.showAllDetails() catch
(ClassNotFoundException ex) System.out.printl
n(ex.toString()) catch (IOException ex)
System.out.println(ex.toString())
Write to file successfully ID 4514452-2 Name
Somchai GPA 3.21
?????????????????????????????
47
????????????????
  • stream ????????????????????????
    ???????????????????? 2 ????????? byte stream ???
    character stream
  • byte stream ???????????????? ???? InputStream ???
    OutputStream
  • character stream ???????????????? ???? Reader ???
    Writer
  • ???? InputStream ??????????????????????? stream
    ??? OutputStream ???????????????????????????????
    stream ?????????????????????????????????? byte

48
????????????????
  • ???? Reader ????????????????????????????????
    stream ???????Writer ?????????????????????????????
    ???? stream ??????????????????????????????????
    char
  • ?????????? stream ??????????????????? ????
    DataInputStream, DataOutputStream,
    BufferedInputStream, BufferedOutputStream,
    PrintStream, PushbackInputStream, BufferedReader,
    BufferedWriter, InputStreamReader,
    OutputStreamWriter, PrintWriter ???
    PushbackReader
  • ???????????? stream ???????? ?????????????????????
    ???????????????????????????????????????????

49
????????????????
  • ???? File ????????????????????????????????????????
    ?? ????????????????????????????????????
  • ???? RandomAccessFile ????????????????????????????
    ???????????? random access
  • ???????????????????????????????? stream
    ?????????????????????????????? implements
    ??????????? Serializable

50
?????????
  • ?????? 1
  • ????????????????????????? ????????????????????????
    ?????????????????????? ???????????????????????????
    ??????????????
Write a Comment
User Comments (0)
About PowerShow.com