Java File Handling Notes
Java File Handling Notes
In Java, a File is an abstract data type. A named location used to store related information is
known as a File. There are several File Operations like creating a new File, getting
information about File, writing into a File, reading from a File and deleting a File.
Before understanding the File operations, it is required that we should have knowledge of
Stream and File methods. If you have knowledge about both of them, you can skip it.
Stream
A series of data is referred to as a stream. In Java, Stream is classified into two types, i.e.,
Byte Stream and Character Stream.
Byte Stream
Byte Stream is mainly involved with byte data. A file handling process with a byte stream is
a process in which an input is provided and executed with the byte data.
Java byte streams are used to perform input and output of 8-bit bytes. Though there are many
classes related to byte streams but the most frequently used classes are, FileInputStream and
FileOutputStream.
import java.io.*;
public class CopyFile {
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Character Stream
Character Stream is mainly involved with character data. A file handling process with a
character stream is a process in which an input is provided and executed with the character
data. Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java
Character streams are used to perform input and output for 16-bit unicode. Though there are
many classes related to character streams but the most frequently used classes are, FileReader
and FileWriter.
import java.io.*;
public class CopyFile {
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Example 1:
import java.io.*;
import java.util.*;
do
itemNo = in.nextInt();
itemName = in.next();
unitPrice = in.nextDouble();
dos.writeInt(itemNo);
dos.writeUTF(itemName);
dos.writeDouble(unitPrice);
System.out.println("Continue(y/n)?");
ch = in.next();
}while(ch.equals("yes"));
dos.close();
}
Example 2:
import java.io.*;
while(dis.available() > 0 ){
itemNo = dis.readInt();
itemName = dis.readUTF();
unitPrice = dis.readDouble();
dis.close();
Example 3:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class ReadFromFile {
try {
while (dataReader.hasNextLine()) {
System.out.println(fileData);
dataReader.close();
exception.printStackTrace();
Serialization
Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is
mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.
For serializing the object, we call the writeObject() method ObjectOutputStream, and for
deserialization we call the readObject() method of ObjectInputStream class.
We must have to implement the Serializable interface for serializing the object.
It is mainly used to travel object's state on the network (which is known as marshaling).
Serializable is a marker interface (has no data member and method). It is used to "mark"
Java classes so that the objects of these classes may get a certain capability. The Cloneable
and Remote are also marker interfaces.
The String class and all the wrapper classes implement the java.io.Serializable interface by
default.
import java.io.Serializable;
Person() {
};
@Override
public String toString() {
return "Name:" + name + "\nAge: " + age + "\nGender: " + gender;
}
}
Filename: WriterReader.java
import java.io.*;
try {
FileOutputStream f = new FileOutputStream(new
File("myObjects.txt"));
ObjectOutputStream o = new ObjectOutputStream(f);
o.close();
f.close();
// Read objects
Person pr1 = (Person) oi.readObject();
Person pr2 = (Person) oi.readObject();
System.out.println(“ ” +pr1);
System.out.println(pr2.toString());
oi.close();
fi.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If a class implements serializable then all its sub classes will also be serializable. Let's see the
example given below:
import java.io.Serializable;
class Person implements Serializable{
int id;
String name;
Person(int id, String name) {
this.id = id;
this.name = name;
}
}
class Student extends Person{
String course;
int fee;
public Student(int id, String name, String course, int fee) {
super(id,name);
this.course=course;
this.fee=fee;
}
}