540 likes | 767 Views
2 . Contents. Files and DirectoriesCharacter StreamsBuffered Character StreamsThe PrintWriter ClassByte StreamsRandom Access FilesThe StreamTokenizer ClassObject SerializationThe Java New I/OWriting FilesReading Files. 3 . Files and Directories. . File(String path)File(String directoryPa
E N D
1. 1
2. 2 Contents Files and Directories
Character Streams
Buffered Character Streams
The PrintWriter Class
Byte Streams
Random Access Files
The StreamTokenizer Class
Object Serialization
The Java New I/O
Writing Files
Reading Files
3. 3 Files and Directories
4. 4 Files and Directories
5. 5 Character Streams
6. 6 Character Streams
7. 7 Character Streams
8. 8 Character Streams
9. 9 Character Streams
10. 10 Character Stream Examples
11. 11 Buffered Character Streams
12. 12 Character Stream Examples
13. 13 The PrintWriter Class
14. 14 Byte Streams (Binary Streams)
15. 15 Byte Streams
16. 16 Byte Streams
17. 17 Byte Streams
18. 18 Byte Streams (DataOutput Interface)
19. 19 Byte Streams (InputStream Interface)
20. 20 Byte Streams (DataInput Interface)
21. 21 Byte Streams (Example)
22. 22 Buffered[Input/Output]Stream Examples
23. 23 DataOutputStream Examples
24. 24 Random Access Files
25. 25 The StreamTokenizer Class
26. 26 StreamTokenizer Example 1
27. 27 StreamTokenizer Example 2
28. 28 Object Serialization What is Object Serialization?
Process of reading and writing objects
Writing an object is to represent its state in a serialized form sufficient to reconstruct the object as it is read.
Object serialization is essential to building all but the most transient applications.
Examples of using the object serialization
Remote Method Invocation (RMI)--communication between objects via sockets
Lightweight persistence--the archival of an object for use in a later invocation of the same program
29. 29 Serializing Objects How to Write to an ObjectOutputStream
Writing objects to a stream is a straight-forward process. Example of constructing a Date object and then serializing that object:
FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject("Today");
s.writeObject(new Date());
s.flush();
30. 30 Serializing Objects How to Read from an ObjectOutputStream
Example that reads in the String and the Date object that was written to the file named theTime in the read example:
FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in);
String today = (String)s.readObject();
Date date = (Date)s.readObject();
31. 31 Serializing Objects Providing Object Serialization for Your Classes
Implementing the Serializable Interface
Customizing Serialization
Implementing the Externalizable Interface
Protecting Sensitive Information
[ObjectFileTest.java]
/home/course/prog3/examples/objserial/ObjectFileTest.java
32. 32 The Java New I/O
33. 33 The Java New File I/O
34. 34 Writing Files
35. 35 Writing Files
36. 36 Writing Files
37. 37 Writing Files
38. 38 Writing Files
39. 39 Writing Files
40. 40 Writing Files
41. 41 Writing Files
42. 42 Writing Files
43. 43 Writing Files
44. 44 Writing Files
45. 45 Writing Files
46. 46 Writing Files
47. 47 Writing Files
48. 48 Writing Files
49. 49 Writing Files
50. 50 Writing Files
51. 51 Reading Files
52. 52 Reading Files
53. 53 Reading Files
54. 54 Exercise Step 1 Creating Information Summarizer (Use the FileInputStream and BufferedReader Class)
Related Slides : #5 – 10
Step 2 (Using the DataInput/OutputStream Class)
Related Slides : #14 – 19
Step 3 (Re-write the Step 2 Using the New I/O package)
Related Slides : #32 – 51
Step 4 (Mixed-Data Processing Using the New I/O package)
Related Slides : #32 – 52
Step 5 (A Stream Tokenizer)
Related Slides : #25-27
Option (Object Serialization)
Related Slides : #28-31