180 likes | 309 Views
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
E N D
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
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.
Exam 1: Closed Book Topics not covered: Jini, Exception Handling, Files/Streams Format: Multiple choice
Java File I/O Focusing on Serialization and Sequential Files [Sources: Deitel, Java 2 Certification, Java 2: The Complete Reference]
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
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)
java.io package • Others: • File • RandomAccessFile • FileDescriptor • ObjectStreamClass • ObjectStreamField • SerializablePermission • SteamTokenizer • Major players: • InputStream • OutputStream • Reader • Writer byte stream character stream (Unicode)
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( )
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
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);
Combining Filters Input Stream Output Stream Filter 1 Filter 2 Filter 3 Filter 4
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); }
File I/O Example (see FileTest.java code on class Web site)
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!
Serializing a Java Object (continued) • Why? • File I/O: Object “data” persistence • JavaBean: reusable software component • Remote Method Invocation (RMI) • Jini
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
Serializing Example (see FileTest2.java code on class Web site)
Web/Networking Example (see Deitel Figure 20.2 of Chapter 20)