0% found this document useful (0 votes)
12 views4 pages

Files and I/O

Uploaded by

Mandar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Files and I/O

Uploaded by

Mandar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Files and I/O

Stream : A stream is a sequence of data.

There are two kinds of Streams

InputStream: Java application uses an input stream to read data from a source,
it may be a file,an array,peripheral device or socket.

OutputStream: Java application uses an output stream to write data to a


destination, it may be a file,an array,peripheral device or socket.

Byte Streams

Java byte streams are used to perform input and output of 8-bit bytes(Binary
Data)

Character Streams

Java Character streams are used to perform input and output for 16-bit
unicode.
Byte Stream Hierarchy

Example 1 : for FileInputStream & FileOutputStream(Read data from source


file & write into destination file)
import java.io.*;
class Demo1{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("old.txt");
FileOutputStream fout=new FileOutputStream("new.txt");
int i=0;
while((i=fin.read())!=-1){
fout.write(i);
}
fin.close();
}
}
Example 2 : for BufferedInputStream & BuffredOutputStream(Read data
from source file & write into destination file)
import java.io.*;
class Demo1{
public static void main(String args[])throws Exception{

FileInputStream fin=new FileInputStream("old.txt");


BufferedInputStream bin=new BufferedInputStream(fin);

FileOutputStream fout=new FileOutputStream("new.txt");


BufferedOutputStream bout=new BufferedOutputStream(fout);

int i=0;
while((i=bin.read())!=-1){
bout.write(i);
}
bin.close();
bout.close();
}
}
Serialization :

Serialization in java is a mechanism of writing the state of an object into a byte


stream.

The reverse operation of serialization is called as deserialization.

java.io.Serializable interface

Serializable is a marker interface (has no body). It is just used to "mark" java


classes which support a certain capability.It must be implemented by the class
whose object you want to persist.

Example:

import java.io.Serializable;
public class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}

Example of Java Serialization

In this example, we are going to serialize the object of Student class. The
writeObject() method of ObjectOutputStream class provides the functionality
to serialize the object. We are saving the state of the object in the file named
f.txt.

import java.io.*;
class Persist{
public static void main(String args[])throws Exception{
Student s1 =new Student(211,"ravi");
FileOutputStream fout=new FileOutputStream("f.txt");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
System.out.println("success persistence");
} }
Example of Java Deserialization

import java.io.*;
class Depersist{
public static void main(String args[])throws Exception{

ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));


Student s=(Student)in.readObject();
System.out.println(s.id+" "+s.name);

in.close();
}
}

You might also like