Title: UMass Lowell Computer Science 91'460 Java and Distributed Computing Prof' Karen Daniels Fall, 2000
1UMass 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
2Homework 3
HW Assigned Due Content
- 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.
3Exam 1 Closed Book
Topics not covered Jini, Exception Handling,
Files/Streams Format Multiple choice
4Java File I/O
- Focusing on
- Serialization and Sequential Files
- Sources Deitel, Java 2 Certification, Java 2
The Complete Reference
5Java 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
6Java Streams
- Motivated by C stream concept
- remember cin gtgt and cout ltlt ?
- 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)
7java.io package
- Major players
- InputStream
- OutputStream
- Reader
- Writer
- Others
- File
- RandomAccessFile
- FileDescriptor
- ObjectStreamClass
- ObjectStreamField
- SerializablePermission
- SteamTokenizer
byte stream
character stream (Unicode)
8java.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( )
9java.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
10Chaining 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) DataOutputS
tream outData new DataOutputStream(outFile)
11Combining Filters
Input Stream
Output Stream
Filter 1
Filter 2
Filter 3
Filter 4
12File I/O Exception Handling
- Try, catch, throw paradigm (remember from C?)
- Some types of exceptions
- IOException when operating on file
- ClassNotFoundException during reading/deserializat
ion - Example
- try output.close( )
- catch (IOException ioex)
-
- JOptionPane.showMessageDialog(null, Error
closing file, Error, JOptionPane.ERROR_MESSAGE)
13File I/O Example
(see FileTest.java code on class Web site)
14Serializing 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!
15Serializing a Java Object (continued)
- Why?
- File I/O Object data persistence
- JavaBean reusable software component
- Remote Method Invocation (RMI)
- Jini
16Serializing 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
17Serializing Example
(see FileTest2.java code on class Web site)
18Web/Networking Example
(see Deitel Figure 20.2 of Chapter 20)