1 / 18

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000. Lecture 13 Java Fundamentals File I/O Serializing an Object Wed. 10/4/00. Homework #3. HW# Assigned Due Content. 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2

hadar
Download Presentation

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000

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. UMass Lowell Computer Science 91.460Java and Distributed ComputingProf. Karen DanielsFall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an Object Wed. 10/4/00

  2. Homework #3 HW# Assigned DueContent 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 2 Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3 Fri, 9/22 Fri, 9/29 Part 1 & Part 2 Graded Homework #4 will be assigned on Fri., 10/6 (skipping one week due to test) Homework is due at the start of lecture on the due date.

  3. Exam 1: Closed Book Topics not covered: Jini, Exception Handling, Files/Streams Format: Multiple choice

  4. Java File I/O Focusing on Serialization and Sequential Files [Sources: Deitel, Java 2 Certification, Java 2: The Complete Reference]

  5. Java I/O Philosophy • Console I/O is limited: • Text-based console programs are not part of the main Java design focus • Applets have limited access to resources • Device-independent abstraction

  6. Java Streams • Motivated by C++ stream concept: • remember cin >> and cout << ? • Stream: • Sequence of bytes or characters that travel from a source to a destination over a communications path • Relieves programmer of burden of communication path details • Abstraction that is device-independent • 2 types: byte and character (Unicode)

  7. java.io package • Others: • File • RandomAccessFile • FileDescriptor • ObjectStreamClass • ObjectStreamField • SerializablePermission • SteamTokenizer • Major players: • InputStream • OutputStream • Reader • Writer byte stream character stream (Unicode)

  8. java.io packageFile class • Used to access file and directory objects • Uses file-naming conventions of host OS • Some methods: • getAbsolutePath( ) • getParent( ) • listFiles( ) • isDirectory( ) • isFile( ) • Example: Using a File constructor • File file = new File(“test.dat”); • exists( ) • getName( ) • length( ) • lastModified( ) • canRead( )

  9. java.io packageInputStream OutputStream classes • InputStream • FilterInputStream • BufferedInputStream • DataInputStream • LineNumberInputStream • PushBackInputStream • ByteArrayInputStream • FileInputStream • ObjectInputStream • PipedInputStream • SequenceInputStream • StringBufferInputStream • OutputStream • FilterOutputStream • BufferedOutputStream • DataOutputStream • PrintStream • ByteArrayOutputStream • FileOutputStream • ObjectOutputStream • PipedOutputStream

  10. FileOutputStream DataOutputStream Chaining Streams The DataOutputStream class is an output filter that allows Strings and primitive data types to be written to a file stream. The FileOutputStream class enables output (stream of bytes) to be written to a file stream. Input Stream Output Stream File file = new File("test.txt"); FileOutputStream outFile = new FileOutputStream(file); DataOutputStream outData = new DataOutputStream(outFile);

  11. Combining Filters Input Stream Output Stream Filter 1 Filter 2 Filter 3 Filter 4

  12. File I/O Exception Handling • Try, catch, throw paradigm (remember from C++?) • Some types of exceptions: • IOException when operating on file • ClassNotFoundException during reading/deserialization • Example: try{ output.close( );} catch (IOException ioex) { JOptionPane.showMessageDialog(null, “Error closing file”, “Error”, JOptionPane.ERROR_MESSAGE); }

  13. File I/O Example (see FileTest.java code on class Web site)

  14. Serializing a Java Object • Serializing: Converting object to a format suitable for stream I/O • Deserializing: Converting serialized object back into object instance. • To serialize, class must implement java.io. Serializable interface • Serialized object contains part of state of an instance: • identifying info for class • instance variables • all reachable non-transient and non-static objects • not the methods!

  15. Serializing a Java Object (continued) • Why? • File I/O: Object “data” persistence • JavaBean: reusable software component • Remote Method Invocation (RMI) • Jini

  16. FileOutputStream ObjectOutputStream Serializing a Java Object (continued) • How? • To serialize an object, its class must implement the Serializable interface • Example: • public class Point2D extends Object implements Serializable {...} • To write/read an object, chain ObjectOutputStream Input Stream Output Stream

  17. Serializing Example (see FileTest2.java code on class Web site)

  18. Web/Networking Example (see Deitel Figure 20.2 of Chapter 20)

More Related