0% found this document useful (0 votes)
3 views

31 Java IO Package

The document provides an overview of Java's IO package, detailing how to perform input and output operations using streams, including byte and character streams. It explains serialization and deserialization processes, emphasizing the importance of the Serializable interface and the handling of transient fields. Additionally, it discusses practical examples of reading from and writing to files, as well as the use of the AutoCloseable interface for resource management.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

31 Java IO Package

The document provides an overview of Java's IO package, detailing how to perform input and output operations using streams, including byte and character streams. It explains serialization and deserialization processes, emphasizing the importance of the Serializable interface and the handling of transient fields. Additionally, it discusses practical examples of reading from and writing to files, as well as the use of the AutoCloseable interface for resource management.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

IO Package

If you are defining any java application then you may get the requirement to read the data or to write
the data. The input output operation can be performed with any device. If you want to perform the
input output task then you need to interact with the corresponding device and to interact with the
device you need to use low level implementation.

KB,File,Network or OtherDevice---input-->JavaAppliction---output--->KB,File,Network or OtherDevice

The low level implementation will be platform and device dependent.

If you change the device or the platform then you need to change the implementation. It will be
maintenance problem for the application.

Sun has verified the problem and has solved by providing some default implementation depending on
the common device. Sun has provided various classes with the implementation. The classes and the
methods in provided in java.io package.

InputDevice------input(java.io)----->JavaAppliction------output(java.io)------->OutputDevice

If you are reading the data or if you are writing the data then the data will be transfered as sequence.
The sequence of the data is known as stream.

Depending on the operation stream will be of two types:-

[1] Input Stream [2] Output Stream

Depending on the data type that will be used in the application stream will be of two types:-

[1] Byte Stream [2] Character Stream

Byte Input Stream

By using this type of stream you can access the value in the java application as Byte type(ASCII value).
java.io.InputStream is the super class for all Byte Input Stream class.

Byte Output Stream

By using this type of stream you can provide the data to the output device from the application as Byte
type(ASCII value). java.io.OutputStream is the super class for all Byte Output Stream class.

Character Input Stream

By using this type of stream you can access the value in the java application as character value.
java.io.Reader is the super class for all Character Input Stream class.
Character Output Stream

By using this type of stream you can provide the value from the application as character. java.io.Writer is
the super class for all Character Output Stream class.

[*] All the super classes are defined as abstract class.

[*] The methods available in the java.io package as throwing one checked exception called
java.io.IOException.

[*] From java7 sun has provided java.lang.AutoCloseable interface that will be used to close the resource
automatically by using try with resource statement.

[*] All the classes used to perform Input Output operation are sub class of java.lang.AutoCloseable.

When the JVM will be started then some object will be created by default. Some object will be used to
perform input output operation with the default input output device. The reference of these values(The
Object) will be available in the variable available in the System class.

public static final java.io.InputStream in;

public static final java.io.PrintStream out;

public static final java.io.PrintStream err;

Input output

=====================

[*] If you want to read the data from file then you can use FileInputStream or FileReader.

[*] If you want to write the data to file then you can use FileOutputStream or FileWriter.

[*] You can use following constructor to create the object to write the content in the file:-

FileOutputStream(String fileName);

FileOutputStream(File file);

FileOutputStream(String fileName, boolean append);

FileOutputStream(File file,boolean append);

FileWriter(String fileName);
FileWriter(File file);

FileWriter(String fileName, boolean append);

FileWriter(File file,boolean append);

[*] To read the data as character you can use BufferedReader class.

[*] The constructor of the BufferedReader is defined with parameter of reader type so
InputStreamReader can not be use to create the object.

[*] You need to use InputStreamReader to read the data from the InputStream and by using this class
you can convert the InputStream into the Reader.
//WAP to read data from user untill return(enter) and print them
import java.io.*;
class IOTest
{
public static void main(String[]st)throws IOException
{
System.out.print("Enter a key:");

while(true)
{
int a=System.in.read();
System.out.println(a+" "+(char)a);
if(a==13)break;
}
}
}
//WAP to create a class to get input from user and access it from other class
import java.io.*;
class MyInput
{
String getInput()throws IOException
{
StringBuffer sb=new StringBuffer();
while(true)
{
int a=System.in.read();
if(a==13)break;
char c=(char)a;
sb.append(c);
}
return sb.toString();
}

class IOTest
{
public static void main(String[]st) throws IOException
{
MyInput mi=new MyInput();
System.out.print("Enter your name:");
String s=mi.getInput();
System.out.println("Name="+s);
}
}
//WAP to create a class to get 2 nos from user and print sum using own input function
import java.io.*;
class MyInput
{
String getInput() throws IOException
{
StringBuffer sb=new StringBuffer();
while(true)
{
int a=System.in.read();
if(a==13)break;
char c=(char)a;
sb.append(c);
}
System.in.read();\\Ignore 13 or \n
return sb.toString();
}

}
class IOTest
{
public static void main(String[]st) throws IOException
{
MyInput mi=new MyInput();
System.out.print("Enter 1st no:");
int a=Integer.parseInt(mi.getInput());
System.out.print("Enter 2nd no:");
int b=Integer.parseInt(mi.getInput());
System.out.println("Sum="+(a+b));
}
}
SERILIAZATION

=====================

It is the process to write the content of the object to the output stream.

[*] If you have the requirement to write the content by encrypting the information that should be
decrypted only through the java application then you can use the serialization. Depending on your
requirement you can serialize the information that may be access by the application of non-java
language.

[*] If you want to serialize the object then you can use java.io.ObjectOutputStream. To write the content
in the file you need to use FileOutputStream.

[*] By default sun has provided some default implementation for serializing the object.

[*] By default static member will not be serialized.

[*] If you are serializing the information then you may get the requirement to get the information
available in the serialized file.

DESERILIAZATION

=============================

[*] Reading the information from the serialized file and creating the actual object in the heap memory is
known as Deserialization.

[*] While deserializing the object the .class file must be available that is used while serializing the object.

[*] While deserializing the object the constructor of the class will not be invoked.

[*] While deserializing static member will be initialized with the value available in the class.

=====================

[*] If you are trying to serialize the data of the object then your class must be the sub type of
java.io.Serializable interface. It is a marker interface that instruct the JVM that the object of the class can
be serialized. If the class is not implementing the Serializable interface then JVM will throw the following
Exception at runtime:-

java.io.NotSerializableException.

[*] If you want to deserialize the object then you can use java.io.ObjectInputStream. To specify the
source as any file you can use java.io.FileInputStream.
Serialization

=====================

[*] If you are defining the object to serialize if the super class is implementing Serializable interface then
the sub class will be by default Serializable.

[*] If any property inherited from the super class that is not implementing Serializable then these
property will not be serialized.

[*] While deserializing that these property will get the default value or the value initialize in the class.

[*] If you have the requirement not to serialize the instance member of the object then you can define
the member as transient. Transient property will not be serialized.

[*] While deserializing the object these property will get the default value.

[*] At the time of deserializing the object the constructor of the serializable class will not be executed. At
the time of deserializing the constructor of the non-serializable super class will be executed.

[*] For the the non-serializable super class the no-argument constructor will be used.

[*] If the object has the any other object as the member then while serializing the custom object the
member object also will be serialized. If the member object class is not implementing Serializable then
JVM will throw java.io.NotSerializableException.

[*] If you don't want to serialize the member object then define the member as transient. If you want to
serialize the class must be the sub type of Serializable interface.

[*] If you have the requirement to customize the serialization process then you can use
java.io.Externalizable interface. It is the sub type of java.io.Serializable interface. It has the following
abstract method:-

public void writeExternal(ObjectOutput out)

public void readExternal(ObjectInput in)

===================================

USE OF SERIALIZATION

===================================

[1] Serialization can be used for memory management. If the object is not being used then we can
serialize the information and we can make the object as unused. The garbage collector will clean the
memory and the memory can be used by other object. Later if required you can access the information
from serialized file and you can create the actual object that can be used by the application.
[2] If you are developing any enterprises application then you may get the requirement to communicate
with the other application executing in other machine. You may get the requirement to provide the data
from one machine to other machine through the network. If you are providing the primitive value then
the actual content will be transferred. If you are providing the reference then the reference of the
object will be transferred, the actual content will not be transferred. To transfer the actual content you
can serialize the object and the serialized information will be transferred to the other machine. In the
next machine you can deserialize the information and you can create the actual object in the
corresponding memory.

You might also like