File and Streams (continued)

hedgebornabaloneΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

93 εμφανίσεις

Chapter
-

12

File and Streams (continued)

This 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





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

DataOuputStream


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 };




String[ ] descs = { "Java T
-
shirt",





"Java Mug",





"Duke Juggling Dolls",





"Java Pin",





"Java Key Chain" };




for (int i = 0; i < prices.length; i ++) {


out.writeDouble(prices[i]);


out.writeChar('
\
t');


out.writeInt(units[i]);


out.writeChar('
\
t');


out.writeChars(descs[i]);


out.writeChar('
\
n');


}


out.close();


}

}



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.



DataInputStream

import 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();


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.





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.






How 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.


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.

Providing Object Serialization for Your Classes


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 {




...



}


Implementing the Serializable Interface