1-1??/??I/O - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

1-1??/??I/O

Description:

Title: UML Author: User Last modified by: User Created Date: 1/6/2006 11:15:31 AM Document presentation format: Company: jjj Other titles – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 47
Provided by: aasirMywe
Category:

less

Transcript and Presenter's Notes

Title: 1-1??/??I/O


1
1-1??/??I/O
  • ????????????????????????input Stream,???????????Ou
    tput Stream??JAVA???????,??????(In)???????(out)???
    ????(err)?System????????(In?out?err)??????????????
    ?public static,???????System?????????????????????,
    ??System.out.println()????????????????????Scanner?
    ??
  • ??I/O??,?????I/O???System.in??????,System.out?Syst
    em.err???????IOException???????,??????catch???????
    ??throws????????I/O?????????????????????????????,?
    ?????I/O?????try..catch????

2
  • ??FileReader_test.java
  • ????text.txt???????
  • ?????java.io.???
  • ???????????FileReader????????input?
  • ??????????trycatchfinally???
  • ?????test.txt??????
  • ???input.read()???????byte???,???code,-1???????
  • ?FileReader??????????????,???????try..catch???????
    java.io.FileNotFoundException?IOException????,???F
    ileNotFoundException??IOException???
  • ??????input.close()???????

3
??FileWriter_test.java
  • ????????good.txt??
  • ???main()?????IOException???
  • ?????FileWriter??output???????good.txt,??????true,
    ??good.txt??????,?????????????,???????,??????,????
    ???????
  • ?????output???????write()?????????
  • ????????,?????????close()???

4
  • ??IO.java
  • ?????java.util.Random???
  • ?????java.io.???
  • ????????????IO???
  • ????main()??,????throws??IOException?
  • ???????MAX?9?
  • ?????????Random()?
  • ?????FileWriter?????fw,FileWriter?????????,???????
    ???????PrintWriter????print()?println()???
  • ????????????????BufferedWriter,??????????
  • ????????????for??,???????????test.txt??

5
1-1-1??
  • ???????,?????????????I/O?????????????????????????,
    ???????????input stream???????????????,??????????o
    utput stream??????????,???????????????????????????
    ?

6
1-1-2????????
  • ??????????????????,???????????????????????ASCII?,?
    ???????????ASCII?????????0?1??????????????????????
    ,??????,??????????????????????????????,???????????
    ?
  • ???????text.txt,??????????????????????????????????
    ,?????????

7
  • ???????????????????????binary.dat?

8
  • ??????????????,???????????,???????????????????????
    ????????????????????,??????????????,??????????????
    ????????????????????,????????????
  • ??????????????????Java?????,??Java????????????????

9
1-2???
  • Java??????????????????????????????Java?,??????????
    ????????Java?,??????????
  • ??????/???????,?????Object???
  • FileWriter?????????,??????????????PrintWriter????p
    rint()?println()??????????BufferedWriter,?????????
    ?
  • Reader???????????,?????????????Writer????????????

10
  • ??java.io.Reader?????????lock???????Object,?????pr
    otected?,???????????????read()????????Close()?????
    ???mark()???????????markSupported()?????????mark()
    ???ready()???????????????reset()??????????

11
  • ??java.io.Writer?????????lock???????Object,?????pr
    otected?,???????????????append()????????????Writer
    ????flush()??????Writer???close()??????????Write()
    ????????

12
1-2-1????
  • PrintWriter??????????????PrintWriter??????print()?
    ??println()??,??????????????????
  • ????FileOutputStream()??????output.txt??????output
    Stream?PrintWriter?????????try..catch???,?????????
    ??trycatch???????????????,???????????????????outp
    ut.txt?????,output.txt???????????FileOutput?????,?
    ????PrintWriter()??????????????????output.txt???Pr
    intWriter?????

13
  • ?????????,FileNotFoundException???????,????????cat
    ch??????,?FileOutputStream???????FileNotFoundExcep
    tion????FileNotFoundException?IOException?????,???
    ???IOException????catch?????
  • ?????????????

14
  • ??????????????,????????????????????close()??,outpu
    tStream.close()??????/?????????close()????????????
    ,????????????????????????????,?????,??????????????
    ??,???????????,?????????????????????????flush()???
    ????????????
  • ??try???????,???????PrintWriter????????try????
    ,??????????????,?????????,???outputStream???

15
  • ??TextFileOutput.java
  • ??????PrintWriter?????outputStream??try..catch????
    ?
  • ????FileOutputStream()???????????????,??????try???
    ?
  • ??????catch??,?????output.txt??,?????FileNotFoundE
    xception??,????????????
  • ?????????????????output.txt????
  • ?????????????outputStream.close()???
  • ??????????
  • ?????output.txt????

16
1-2-2????
  • BufferReader??????????????BufferedReader???PrintWr
    iter????,???????????????,??????FileReader?????????
    BufferedReader??????
  • ?????????????
  • ??????????????,?????????char?????
  • char next(char)(input.read())

17
  • ??TextFileInput.java
  • ?????java.io.BufferedReader??,????????java.io????
  • ?????????FileReader()?????,???????test.txt???Buffe
    redReader()?????????????,?????????
  • ???inputStream.readLine()???????
  • ????inputStream.close()????????
  • ??????????try..catch??,???????????????????????????
    ?,?????????????,?????

18
  • ????????test.txt?
  • ??????????

19
1-2-3?????????
  • ?????BufferedReader???,?????readline()???read()???
    ????????readline()????????,????null??????read()???
    ?????,????-1?
  • ??TextEOF.java
  • ??????????BufferedReader()??,??first.txt??????
  • ???????????PrintWriter()??,??????second.txt???
  • ??????lineContent???????????????
  • ??????????while??,???????,???????outStream,??????l
    ineContent???null?
  • ????????????
  • ????????????

20
  • ??????first.txt??????
  • ??????second.txt??????

21
1-2-4Systme.in?System.out?System.err
  • System.in?System.out?System.err?????Java??????????
    ???????System.out?System.in?System.err???????????J
    ava??????????????????????,??????System???????setIn
    ()?setOut()?setErr()???????????errStream?????,????
    ?System.err????errStream?????????,??????System.set
    Err(errStream)?
  • ?????????error???
  • System.err.println(System.err)

22
  • ??Redirection.java
  • ?????PrintStream??????,?????FileOutputStream??,???
    ?error.txt???
  • ??????System???????setErr(),???????error???errorSt
    ream???
  • ????errorStream.close()?????errorStream?

23
1-2-5????
  • File???????????????????????File???????????????????
    ,???????????????Windows???c\good\test.dat,?Java??
    ??????c\\good\\test.dat?
  • new File(c\\good\\test.dat)????c\\good\\test.d
    at????

24
(No Transcript)
25
  • ??FileClass.java
  • ??????????????????keyboard?
  • ?????null?line???
  • ????keyboard.readLine()??????????
  • ????keyboard.readLine()?????????????
  • ?????????File???
  • ???????exists()??????????,?????,??????????????
  • ?????????????????????

26
(No Transcript)
27
(No Transcript)
28
  • ???????????good.txt????,??good.txt????,?????????
    ????????????????????????

29
1-3?????/??
  • ???????????????????????????????????int???????,??0?
    1??????????,??????????,???????,??????????????
  • Java??????????Java????????,???????????????????????
    ????????ObjectInputStream?ObjectOutpustStream?????
    ?????????????????????????????,???????????
  • ???????byte?I/O???InputStream?????byte???????????O
    utputStream?????????????InputStream/OutputStream??
    ?Reader/Writer?????????,?????????????byte??/??,???
    ?????/??????????????

30
  • ??BinaryOutput.java
  • ?????ObjectOutputStrem??,?FileOutputStream??????
  • ???????outputSream.writeInt(i),??????0?257?outputS
    ream????????????????

31
  • ??????Binary.dat??????byte?8?bits???????,???????,?
    0?258?

32
  • ??DataStreamTest.java
  • ?????DataOutputStream??,???FileOutputStream???????
    ,??????tmp.dat???
  • ????UTF????????Mary???outputStream????
  • ????Double??????????55.5???outputStream????
  • ????outputStream?????close()?
  • ???????????DataInputStream??,?FileInputStream?????
    ?
  • ??????inputStream?????,??readUTF()???
  • ????????
  • ??tmp.dat???????

33
  • ??BufferedDataStreamTest.java
  • ????????BufferedOutputStream?BufferedInputStream??
    ??BufferedInputStream?BufferedOutputStream????????
    ???,?????????????????????????BufferedReader?Buffer
    edWriter?????????BufferedInputStream?BufferedOutpu
    tStream????????byte????

34
1-3-1????????
  • ObjectOutputStream?????????????ObjectOutputStream?
    ????????????????????????????????????I/O?????????IO
    Exception???
  • ??????Binary.dat????????,????????
  • ??????????????????ObjectOutputStream?????,????????
    ????FileOutputStream????FileNotFoundException??,??
    ??IOException??????IOException?????????????

35
(No Transcript)
36
1-3-2???????????
  • ObjectIntputStream?????????????ObjectIntputStream?
    ????????????????????????????????????I/O?????????IO
    Exception???
  • ??????????????????ObjectInputStream?????,?????????
    ???FileInputStream????FileNotFoundException??,????
    IOException??????IOException?????????????

37
  • ??BinaryInput.java
  • ?????????ObjectInputStream??????,???FileInputStrea
    m????????
  • ?????????????????
  • ??????????inputStream?

38
1-3-3??????????
  • ??ObjectInputStream?????????????,????EOFException?
    ??
  • ??BinaryEof.java
  • ?????????????try..catch?????????????while??,????Bi
    nary.dat???????,?????????,??,????EOFException??,??
    ?????catch????,?????????????
  • ????????????inputStream.close()?

39
1-3-4???????/??
  • ?????????????/?????,???????????Serializable???????
    ObjectOutputStream???writeObject()????????????????
    ?????writeObject()??????????????,??????readObject(
    )?????????,???????????????????readObject()??????Ob
    ject?????
  • implements??Serializable???????serializable???????
    ????????????????wirteObject()?readObject()????????
    Serializable??,?????????ObjectOutputStream??????

40
  • ??MyClass.java
  • ?????java.io.Serializable?????
  • ????MyClass??,????Serializable??,?????????ObjectOu
    tputStream??????

41
  • ??ObjectIO.java
  • ????????????????ObjectOutputStream?
  • ????????oneObject???
  • ????????anotherObject???
  • ??????outputStream???????writeObject()??,?oneObjec
    t??????????
  • ???????????????????ObjectInputStream?
  • ??????????inputStream???readObject()??,?????????My
    Class???????readObject()??????Object?????
  • ????inputStream?????readObject()??,?????????MyClas
    s???????readObject()??????Object???????one?two???

42
1-3-5???????????
  • ????????????,?????????implements???serializable,??
    ???????writeObject()??????????readObject()???readO
    bject()??????Object?????,??????????????????

43
  • ??ArrayIO.java
  • MyClass??????????,????serializable???
  • ?????MyClass?????a???????????????????
  • ????????????ObjectOutputStream???????
  • ?????a?????writeObject()?????outputStream??????
  • ?????????MyClass??????b?
  • ?????????????inputStream?
  • ??????inputStream??????????,??????MyClass?????,???
    ????b?????
  • ????????????????b??????

44
1-3-6???????
  • ?????????????????,??????RandomAccessFile?????File
    pointer????????????????,??????seek()?????????File
    pointer???????
  • java.io.RandomAccessFile?????????

45
  • ??TestRandomAccess.java
  • ?????????????????RandomAccessFile???ioStream?
  • ????1???ioStream????????
  • ??????ioStream.length()???????????
  • ??????ioStream.getFilePointer()???????????????
  • ??????ioStream.seek(1)????????????????1????
  • ???????writeByte(9)???9??????1????
  • ???????????????????
  • ???????????????55.55??ioStream???
  • ????ioStream.length()???ioStream??????
  • ???????????????????????,?????????????????

46
  • ?????????????????8?bytes???,????????1?byte???,???1
    1?bytes?
Write a Comment
User Comments (0)
About PowerShow.com