Title: File and Streams continued
1Chapter - 12
File and Streams (continued)
2This chapter includes -
- DataOutputStream
- DataInputStream
- Object Serialization
- Serializing Objects
- How to Write To an ObjectOutputStream
- How to Read From an ObjectInputStream
- Providing Object Serialization For Your Classes
- Implementing The Serializable Interface
3DataOuputStream
- What is a DataOutputStream ?
- It is a ByteStream
- It can Read Different Datatypes.
- How to Open a DataOutputStream
- First Open a OutputStream such as
FileOutputStream to the Source - Then open a DataOutputStream with the
OutputStream. - How to Read from DataOutputStream
- DataOutputStream has writeXXX methods to read
different data types. - Solve the problem
- Let us store some tabular data in invoice1.txt.
The tabular data is formatted in columns, where
each column is separated from the next by tabs.
The columns
4- contain the sales price, the number of units
ordered, and a description of the item,like this
- 19.99 12 Java T-shirt
- 9.99 8 Java Mug
- import java.io.
- public class DataIOTest
- public static void main(String args) throws
IOException - // write the data out
- DataOutputStream out new
DataOutputStream(new - FileOutputStream("invoice1.txt"))
- double prices 19.99, 9.99, 15.99,
3.99, 4.99 - int units 12, 8, 13, 29, 50
5 String descs "Java T-shirt", "Java
Mug", "Duke Juggling Dolls", "Java
Pin", "Java Key Chain"
for (int i 0 i lt prices.length i )
out.writeDouble(pricesi)
out.writeChar('\t')
out.writeInt(unitsi)
out.writeChar('\t')
out.writeChars(descsi)
out.writeChar('\n')
out.close()
6DataInputStream
- What is a DataInputStream ?
- It is a ByteStream
- It can Write Different Datatypes.
- How to Open a DataInputStream
- First Open a InputStream such as FileInputStream
to the Source - Then open a DataOutputStream with the
OutputStream. - How to Read from DataOutputStream
- DataOutputStream has readXXX methods to read
different data types. - Solve the problem
- Let us now read the invoice1.txt from previous
example to read the dataitems written by previous
to display a statement summarizing the order and
the total amount owed.
7import java.io. public class DataIOTest2
public static void main(String args) throws
IOException DataInputStream in new
DataInputStream(new FileInputStream("invoice1
.txt")) double price int
unit String desc double total
0.0 try while (true)
price in.readDouble()
in.readChar() // throws out the tab
unit in.readInt()
in.readChar() // throws out the tab
desc in.readLine()
8 System.out.println("You've ordered "
unit " units of " desc " at "
price) total total unit
price catch
(EOFException e)
System.out.println("For a TOTAL of " total)
in.close()
Object Serialization
- Can we read or write an object at a time ?
- Yes, Two streams in java.io-- ObjectInputStream
and ObjectOutputStream provide this facility. -
9- The key to writing an object is to represent its
state in a serialized form sufficient to
reconstruct the object as it is read. - Thus reading and writing objects is a process
called object serialization.
Serializing Objects
- Reconstructing an object from a stream requires
that the object first be written to a stream. So
let's start there
How to write to an ObjectOutputStream
- Step 1
- Open a OutputStream such as FileOutputStream to
the Source - Step 2
- Then open a ObjectOutputStream on that
OutputStream. - How to write object to ObjectOutputStream
- ObjectOutputStream has writeObject (Object o)
method which can write supplied object. - Finally close the ObjectOutputStream with close()
method.
10How to read From a ObjectInputStream
- Step 1
- Open an InputStream such as FileInputStream to
the Source - Step 2
- Then open a ObjectInputStream on that
InputStream. - How to read object from ObjectInputStream
- ObjectInputStream has readObject method which
reads an object and returns that. - Finally close the ObjectOutputStream with close()
method.
11Providing Object Serialization for Your Classes
- How to make an object Serializable ?
- An object is serializable only if its class
implements the Serializable interface. - Thus, if you want to serialize the instances of
one of your classes, the class must implement the
Serializable interface. - Good News about Serializable Interface!
- The good news is that Serializable is an empty
interface. That is, it doesn't contain any method
declarations. - What is the purpose of an empty Interface
- it's purpose is simply to identify classes whose
objects are serializable.
12Implementing the Serializable Interface
- Making instances of your classes serializable is
easy. - You just add the implements Serializable clause
to your class declaration like this - public class MySerializableClass implements
Serializable - ...
-