Download presentation
Presentation is loading. Please wait.
Published byCory Bryan Modified over 9 years ago
1
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
2
Homework #3 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 HW# Assigned Due Content Homework is due at the start of lecture on the due date. Homework #4 will be assigned on Fri., 10/6 (skipping one week due to test)
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 > 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 ä Major players: ä InputStream ä OutputStream ä Reader ä Writer ä Others: ä File ä RandomAccessFile ä FileDescriptor ä ObjectStreamClass ä ObjectStreamField ä SerializablePermission ä SteamTokenizer byte stream characterstream(Unicode)
8
java.io package File 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 package InputStream OutputStream classes ä InputStream ä FilterInputStream ä BufferedInputStream ä DataInputStream ä LineNumberInputStream ä PushBackInputStream ä ByteArrayInputStream ä FileInputStream ä ObjectInputStream ä PipedInputStream ä SequenceInputStream ä StringBufferInputStream ä OutputStream ä FilterOutputStream ä BufferedOutputStream ä DataOutputStream ä PrintStream ä ByteArrayOutputStream ä FileOutputStream ä ObjectOutputStream ä PipedOutputStream
10
Chaining Streams FileOutputStreamDataOutputStream File file = new File("test.txt"); FileOutputStream outFile = new FileOutputStream(file); DataOutputStream outData = new DataOutputStream(outFile); The FileOutputStream class enables output (stream of bytes) to be written to a file stream. The DataOutputStream class is an output filter that allows Strings and primitive data types to be written to a file stream. Input Stream Output Stream
11
Combining Filters Filter 1 InputStreamOutput Stream Stream 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); 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
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 FileOutputStreamObjectOutputStream 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.