Input-Output Fundamentals in JAVA 5


  • Saving the object to some type of permanent storage is called as persistence.
  • An object is said to be persistent capable when you can store that object on a disk or a tape or send it to another machine to be stored in memory or on disk.
  • The non persisted object exists only as long as the Java Virtual Machine is running.
  • The Serialization is a mechanism for saving the objects as a sequence of bytes and later, when needed, rebuilding the byte sequence back into a copy of the object.
  • For object of a specific class to be serializable, a class must implement the  interface, i.e.
  • The Serializable interface has no methods ans only serves as a marker that indicates that the class that implements the interface can be considered for serialization.

Serialization and Object Graph:-

  • When the object is serialized, only the fields of the object are preserved; methods and constructors are not part of the serialized stream.
  • When a field is a reference to an object, the field of that referenced objects are also serialized if that objects class is serializable.
  • The tree, or structure of an object’s fields, including these sub objects, constitutes the object graph.
  • Some of the object classes are not serializable because the data they represent contain references to transient operating system resources.
  • For example, and java.lang.Thread classes.
  • If the serializable object contains a reference to a non serializable element, the entire serialization operation fails and a NotSerializableException is thrown.
  • If the object graph graph contains a non serializable object reference, the object can still be serialized if the reference is marked with the transient keyword.
  • The code given below shows an example of a class that implements the Serializable interface.


  1. public class MyClass implements Serializable
  2. {
  3. public transient Thread myThread;
  4. private String customerID;
  5. private int total;
  6. }


  • The field access modifier (public, protected, default and private) has no effect on the data field being serialized.
  • The data is written to the stream in byte format and with strings represented as file system safe universal character ser transformation format (UFT) characters.
  • A transient keyword prevents the data from being serialized.

Note: – The values stored in static fields are not serialized. When the object is desterilized the values of the static fields are set to the values stored in the corresponding class variables.

Writing and reading an Object Stream:-

  • Writing and Reading an object to a stream is a simple process. In this we provide example of writing and reading from an object stream.



The code fragment contained in given code sends an instance of a java.util.Date object to a file.


  1. import*;
  2. import java.util.Date.*;
  3. public class SerializeDate
  4. {
  5. SerializeDate()
  6. {
  7. Date d = new Date();
  8. try{
  9. FileOutputStream f = new FileOutputStream(“date11.ser”);
  10. ObjectOutputStream s = new ObjectOutputStream(f);
  11. s.writeObject(d);
  12. s.close();
  13. }catch(IOException e){
  14. e.printStackTrace();
  15. }
  16. }
  17. public static void main(String args[])
  18. {
  19. new SerializeDate();
  20. }
  21. }


  • The serialization starts at line 14 when writeObject() method is invoked.


Java Object Serialization



Reading the object is as simple as writing it, but with one caveat the readObject() method returns the stream as an Object type, and it must be cast to the appropriate class name before methods on that class can be executed. The code given below illustrates how to desterilize data from a stream.


  1. import*;
  2. import java.util.Date.*;
  3. public class DeSerialieDate
  4. {
  5. DeSerializeDate()
  6. {
  7. Date d = null;
  8. }
  9. try{
  10. FileInputStream f = new FileInputStream(“date11.ser”);
  11. ObjectInputStream s = new ObjectInputStream(f);
  12. d = (Date) s.readObject();
  13. s.close()
  14. }catch(Exception e){
  15. e.printStackTrace();
  16. }
  17. System.out.println(“Deserialized Date object from date11.ser”);
  18. System.out.println(“Date: ”+d);
  19. }
  20. public static void main(String args[])
  21. {
  22. new DeSerializeDate();
  23. }
  24. }


  • The object deserialization occurs at line 15, when the readObject() method is invoked.


Basic Character Stream Classes:-

The InputStreamReader and OutputStreamWriter Methods:-

  • The most important versions of readers and writers are InputStreamReader and OutputStreamWriter.
  • These classes are used to interface between byte streams and character readers and writers.
  • When you construct an InputStreamReader or OutputStreamWriter, conversion rules are defined to change between 16 bit Unicode and other platform specific representations.

Byte and Character Conversions:-

  • By default, if you construct a reader or writer connected to a stream, the conversion rules change between bytes using the default platform character encoding used is International Organization for Standardization (ISO) 8859-1.
  • Specify an alternative byte encoding by using one of the supported encoding forms.
  • Using this conversion scheme, Java technology uses the full flexibility of the local platform character set while still retaining platform independence through the internal use of Unicode.

Using Other Character Encoding:-

  • If you need to read inputs from a character encoding that is not your local one (for example, reading from a network connection with a different type of machine), you can construct the InputStreamReader with an explicit character encoding such as:


InputStreamReader ir = new InputStreamReader(, “ISO-8860-2”);


Note: – if you are reading characters from a network connection, use this from. If you do not, your program will always attempt to convert the characters it reads as if they were in the local representations, which is probably not correct. ISO 8859-1 is the Latin-1 encoding scheme that maps onto ASCII.

The FileReader class and FileWriter Class:-

  • The FileReader and FileWriter classes are node streams that are the Unicode character analogous of the FileInputStream and FilepOutputStream classes.

The BufferedReader class, and the  BufferedWriter Class:-

  • Use the BufferedReader and BufferedWriter Class filter character streams to increase the efficiency of I/O operations.

The StringReader class and StringWriter Class:-

  • The StringReader,  StringWriter these classes are node character streams that read from or write to Java technology String objects.
  • Suppose that you wrote a set of report classes that contains methods that accept a Writer parameter (the destination of the report text).
  • Because the method makes calls against a generic interface, the program can pass in a FileWriter object or a StrongWriter object; the method code does not care.
  • You use the former object to write the report into memory within a String to be displayed within a GUI text area.
  • In either case, the report writing code remains the same.

The PipedReader class and PipedWriter Class:-

  • You use piped streams for communicating between threads. A PipedReader object in one thread receives its input from a complementary PipedWriter object in another thread.
  • The piped streams must have both an input side and an output side to be useful.

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.


Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply