File Handling
File Handling
File is a collection of related data. Files stores data permanently. With the help of file large
amount of data can be stored in secondary storage devices in place of Main memory.
Java Streams Stream classes are used for input and output purposes are contained in java.io package.
There are 2 types of stream classes in java
(i) Byte Stream Classes used for Byte oriented IO and are categorized into Input Stream
Classes & Output Stream Classes
(ii) Character stream classes used for character oriented IO and categorized into Reader
Classes & Writer Classes
Text Files : In text files data are stored as per a specific character encoding scheme e.g
ASCII text or Unicode text is stored.
Binary Files: In binary files data is stored in the form of bytes that are in machine readable
form.
Buffer is a temporary storage used to hold data until enough has been collected that is
worth transferring.
Deletion of files
File P=new File("students.txt");
File Q=new File("temp");
P.delete(); //delete file students.txt
Q.renameTo(P);//to rename file temp to students.txt
Appending in files
FileWriter fw=new FileWriter("names.txt",true); // content will be appended
For Creation and writing into text File following classes are used
FileWriter.
BufferedWriter
PrintWriter
To Read from text File following classes are used
FileReader
BufferedReader
Ques: WAP to create a text file names.txt to write name , age and roll no of 5 students.
For Creation and writing into Binary File following classes are used
FileOutputStream
DataOutputStream
Ques : Create a binary file student.dat to store name, roll no, marks of 5 students
import java.io.*;
public class WriteinBinary
{
public void main()throws IOException
{
BufferedReader p=new BufferedReader( new InputStreamReader (System.in));
String name;
int rno;
float marks;
FileOutputStream A=new FileOutputStream("student.dat");
DataOutputStream B=new DataOutputStream(A);
for(int i=0;i<5;i++)
{
System.out.print("Enter Name: ");
name=p.readLine();
System.out.print("Enter Roll no: ");
rno=Integer.parseInt(p.readLine());
System.out.print("Enter Marks :");
marks=Float.parseFloat(p.readLine());
B.writeUTF(name);
B.writeInt(rno);
B.writeFloat(marks);
}
B.close();
}
}
OUTPUT
Name :AAA
Roll no :1
Marks :97.5
Name :BBB
Roll no :2
Marks :98.5
Name :CCC
Roll no :3
Marks :56.5
Name :DDD
Roll no :4
Marks :45.5
Name :EEE
Roll no :5
Marks :100.0
WAP to read data from the existing binary file student.dat
import java.io.*;
class readfromBinary_1
{ public void main()throws IOException
{
FileInputStream X=new FileInputStream("student.dat ");
DataInputStream Y=new DataInputStream(X);
String n;
int rno;
float marks;
boolean EOF=false;
System.out.println("Name\tRollno\t Marks ");
while(!EOF)
{
try
{
n=Y.readUTF();
rno=Y.readInt();
marks=Y.readFloat();
System.out.println( n+" \t"+rno+" \t"+marks);
}
catch(Exception e)
{
EOF=true;
}
}
Y.close();
}
}
OUTPUT
Name Rollno Marks
AAA 1 97.5
BBB 2 98.5
CCC 3 56.5
DDD 4 45.5
EEE 5 100.0
(Delete)
WAP to delete all the records of the students from the existing binary file
student.dat who have scored less than 90 marks.
import java.io.*;
public class deletefromfile
{
public void main()throws IOException
{
String n;
int rno;
float marks;
FileInputStream A=new FileInputStream("student.dat");
DataInputStream B=new DataInputStream(A);
FileOutputStream X=new FileOutputStream("temp");
DataOutputStream Y=new DataOutputStream(X);
boolean EOF=false;
while(!EOF)
{
try
{
n=B.readUTF();
rno=B.readInt();
marks=B.readFloat();
if(marks>=90)
{
Y.writeUTF(n);
Y.writeInt(rno);
Y.writeFloat(marks);
}
}
catch(Exception e)
{
EOF=true;
}
}
B.close();
Y.close();
File P=new File("student.dat");
File Q=new File("temp");
P.delete();
Q.renameTo(P);
}
}
Output when the content of the file student.dat will be read using file reading program after
executing the above program
(Update)
WAP to increase roll no of all the students of the existing binary file student.dat
by 100.
import java.io.*;
public class fileUpdate
{
public void main()throws IOException
{
String n;
int rno;
float marks;
FileInputStream A=new FileInputStream("student.dat");
DataInputStream B=new DataInputStream(A);
FileOutputStream X=new FileOutputStream("temp");
DataOutputStream Y=new DataOutputStream(X);
boolean EOF=false;
while(!EOF)
{
try
{
n=B.readUTF();
rno=B.readInt();
marks=B.readFloat();
Y.writeUTF(n);
Y.writeInt(rno+100); Output when the content of the file student.dat
Y.writeFloat(marks); will be read using file reading program after
} executing the above program
catch(Exception e)
{ Name Rollno Marks
EOF=true; AAA 101 97.5
} BBB 102 98.5
} EEE 105 100.0
B.close();
Y.close();
File P=new File("student.dat");
File Q=new File("temp");
P.delete();
Q.renameTo(P);
}
}
WAP to create two binary files “isoar” and “remedial” and transfer(copy) records of all
the student from the binary file “student.dat" who have scored 90 or more marks into
“isoar” file and less than 60 marks into “remedial” file.
import java.io.*;
public class isoar_remedial
{
public void main()throws IOException
{
String n;
int rno;
float marks;
FileInputStream A=new FileInputStream("student.dat");
DataInputStream B=new DataInputStream(A);
FileOutputStream X=new FileOutputStream("isoar");
DataOutputStream Y=new DataOutputStream(X);
FileOutputStream P=new FileOutputStream("remedial");
DataOutputStream Q=new DataOutputStream(P);
boolean EOF=false;
while(!EOF)
{
try
{
n=B.readUTF();
rno=B.readInt();
marks=B.readFloat();
if(marks>=90)
{
Y.writeUTF(n);
Y.writeInt(rno);
Y.writeFloat(marks);
}
if(marks<60)
{
Q.writeUTF(n);
Q.writeInt(rno);
Q.writeFloat(marks);
}
}
catch(Exception e)
{
EOF=true;
}
}
B.close();
Y.close();
Q.close();
}
}