1 / 50

Chapter 4 Java I/O

Chapter 4 Java I/O. x.zhang@seu.edu.cn. Content. Java I/O Introduction File and Directory Byte-stream and Character-stream Bridge between b-s and c-s Random Access File Standard I/O System.in System.out java.nio Pilot. Java I/O Introduction. I/O Target File Console

keanu
Download Presentation

Chapter 4 Java I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 JavaI/O x.zhang@seu.edu.cn

  2. Content • Java I/OIntroduction • File and Directory • Byte-stream and Character-stream • Bridge between b-s and c-s • Random Access File • Standard I/O • System.in • System.out • java.nioPilot

  3. Java I/OIntroduction • I/OTarget • File • Console • Network Connection • I/OManner • Text-based (char) / Data-based(byte) • Sequential / Random Access CoSE Java 2009

  4. Java I/OIntroduction • java.io Package • general classes • filtered / buffered / piped streams • data streams • File • object serialization CoSE Java 2009

  5. File and Directory • java.io.File - “A Path in a file system” • File • Directory • FileConstruction CoSE Java 2009

  6. File and Directory • Example: Directory Lister • List *.log from“c:\Windows”in descending order • Think: How to code it in C++ CoSE Java 2009

  7. File and Directory • Design of Classes CoSE Java 2009

  8. CoSE Java 2009

  9. How to write DirFilter?

  10. A E C B F D Stream • The Notion of Stream • A sequence of flowing byte / char • A channel sending message in FIFO In Out

  11. Stream • Classification of Stream • Byte Stream • Byte as the unit • Used to read and write binary data • Character Stream • Char as the unit • Used to read and write text

  12. Stream • Abstract Stream Class in java.io • Byte stream • java.io.InputStream • read() //read a byte • java.io.OutputStream • write() //write an int ?? • Character stream • java.io.Reader • read() //read a char • java.io.Writer • write() //write an int ??

  13. Stream • Implemented Classes in java.io • Byte stream • FileInputStream、FileOutputStream • *PipedInputStream、PipedOutputStream • ByteArrayInputStream、 ByteArrayOutputputStream • BufferedInputStream、BufferedOutputStream • ObjectInputStream、ObjectOutputStream • Character stream • FileReader、FileWriter • *PipedReader、PipedWriter • BufferedReader、BufferedWriter • InputStreamReader、OutputStreamWriter

  14. Byte Stream • A Complex Hierarchy of Byte Stream CoSE Java 2009

  15. Byte Stream • FileInputStream • Read bytes from file system • Used to read image or data • FileOutputStream • Write bytes to file system • Used to write image or data

  16. Byte Stream • Example: • Write following data into “c:\test.dat” • Read them out • byte 97 • char ‘b’ • String “好” CoSE Java 2009

  17. CoSE Java 2009

  18. Byte Stream • Think • What is the result of this program? • If the parameter of read() and write() out of range of Byte, what will happen?

  19. Character Stream • FileReader • Read char from file • FileWriter • Write char to file • FileReaderand FileWriter use system default encoding • Use other encodings: • InputStreamReader • OutputStreamWriter CoSE Java 2009

  20. Character Stream • Example: • Write following chars • Read them out • ‘C’‘o’‘S’‘E’ • ‘软’‘件’‘学’‘院’ CoSE Java 2009

  21. CoSE Java 2009

  22. Byte Stream and Character Stream • Byte Stream • An int or byte[] can be written to an OutputStream; • An int or byte[] can be read from an InputStream; • Character Stream • An int or char[] or String can be written to an Writer; • An int or char[] or CharBuffer can be read from a Reader

  23. Byte Stream and Character Stream • Think • How to input a student information into a file? • Student ID(int) • Name(String) • Age(short) • Sex(boolean) • How to read these information from file? • How to store these information in binary or text? CoSE Java 2009

  24. Self-study • PrintStream(will be used in Chapter 11) • Inherited from OutputStream • DataInputStreamand DataOutputStream • Inherited from InputStreamand OutputStream • PrintWriter • Inherited from Writer • Scanner • java.util.Scanner CoSE Java 2009

  25. Buffered Stream • Add buffer for input and output. • To improve the efficiency, read() and write() is not invoked immediately, but after the buffer is full. • Buffer is implemented using inner array. • Usually, buffered stream is connected to other streams (such as FileInputStream) • flush() CoSE Java 2009

  26. Buffered Stream • Example: Benchmarkfollowing classes • FileInputStream vs. BufferedInputStream; • FileOutputStream vs. BufferedOutputStream; CoSE Java 2009

  27. CoSE Java 2009

  28. Bridge Between B-S and C-S • InputStreamReader • OutputStreamWriter

  29. Random Access File • java.io.RandomAccessFile • RandomAccessFileis used for fixed length records • Using seek(long postion)to locate • Nothing to do with InputStream and OutputStream • Like DataInputStreamand DataOutputStream • Often used for building index of Search Engines

  30. Standard I/O • System.in • InputStream • Input from keyboard • System.out • PrintStream ->FilterOutputStream -> OutputStream • Show information in console • System.err • PrintStream ->FilterOutputStream -> OutputStream • Show error information in console CoSE Java 2009

  31. CoSE Java 2009

  32. Best Practice • Common Programming Errors • Use FileOutputStream to write to an existing file – the existing content will be erased. • Path errors - \ and \\ • Good Programming Habits • Choose mode r for read-only for RandomAccessFile • Judge the existence of a file before using FileOuputStream • Use buffer as possible as you can • Remember to close the stream

  33. java.nioPilot • Motivation:High Speed I/O • Mapped Memory File

  34. java.nioPilot • Example: • Use MappedByteBuffer to create a 128Mfile, with each byte be a binary representation of ‘x’ • Read 6 bytes from the file CoSE Java 2009

  35. CoSE Java 2009

  36. Lab Work • Homework:Benchmark using abstract class: • Memory-mapped file vs. DataInputStream • Memory-mapped file vs. DataOutputStream • Memory-mapped file vs. RandomAccessFile • Tips: CoSE Java 2009

  37. Lab Work • Tips • DataOutputStream • DataInputStream CoSE Java 2009

  38. Lab Work • RandomAccessFile • Mapped File CoSE Java 2009

  39. Lab Work • Setup an Benchmark Environment • Design a Benchmark Case • Run and Gain the Efficient of Each I/OClass • * Evaluate and Analysis the Performance Curve CoSE Java 2009

  40. Self-study • Serializable Object • Object serialization CoSE Java 2009

  41. Self-study • Reading • The.Java.Programming.Language.4th.Edition, Chapter 20; • Thinking in Java, 3th Edition, Chapter 12. CoSE Java 2009

  42. Forecast • Arrays • Collection • ArrayList • LinkedList • Map • HashMap • Iterator CoSE Java 2009

More Related