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

File Handling

Uploaded by

batmanop005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

File Handling

Uploaded by

batmanop005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

File Handling in Java

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.

Text File Handling


FileWriter : for opening & giving name to the file
BufferedWriter : creates buffer for the stream created with FileWriter.
PrintWriter Writing content from buffer to the file.

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.

import java .io.*;


public class writeinfile
{ BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
public void main()throws IOException
{
int a,r;
String name;
FileWriter fw=new FileWriter("students.txt");
BufferedWriter bw=new BufferedWriter(fw); // created with FileWriter
PrintWriter outFile=new PrintWriter(bw);
for(int i=0;i<5;i++)
{
System.out.print("enter name");
name=stdin.readLine();
System.out.print("enter age");
a=Integer.parseInt(stdin.readLine());
System.out.print("roll no");
r=Integer.parseInt(stdin.readLine());
outFile.println(name);
outFile.println(a);
outFile.println(r);
}outFile.close();
}
}
-----------------------
Ques : WAP to read an existing file text file name.txt to write name , age and roll no of 5 students.

//READ name ,age ,roll no from file students.


import java.io.*;
class readfromfile
{
public void main( )throws IOException
{
System.out.println("******************************");
FileReader x=new FileReader("names.txt");
BufferedReader y =new BufferedReader(x);
String n;
int i=0,a,r;
while( (n=y.readLine())!=null)
{
a=Integer.parseInt(y.readLine());
r=Integer.parseInt(y.readLine());
System.out.println("name of student : "+n+"\n age : "+a+"\n rollno
: "+r); }
y.close();
}
}
BINARY FILE HANDLING

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

Enter Name: AAA


Enter Roll no: 1
Enter Marks :97.5
Enter Name: BBB
Enter Roll no: 2
Enter Marks :98.5
Enter Name: CCC
Enter Roll no: 3
Enter Marks :56.5
Enter Name: DDD
Enter Roll no: 4
Enter Marks :45.5
Enter Name: EEE
Enter Roll no: 5
Enter Marks :100.0
To Read from Binary File following classes are used
FileInputStream
DataInputStream

WAP to read data from the existing binary file student.dat


import java.io.*;
public class BinaryInput
{ public void main()throws IOException
{
FileInputStream fr=new FileInputStream("student.dat");
DataInputStream dr=new DataInputStream(fr);
String n;
int rno;
float marks;
boolean EOF=false;
while(!EOF)
{
try
{
n=dr.readUTF();
System.out.println("Name :"+n);
rno=dr.readInt();
System.out.println("Roll no :"+rno);
marks=dr.readFloat();
System.out.println("Marks :"+marks);
}
catch(Exception e)
{ EOF=true;
}
}
dr.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

Name Rollno Marks


AAA 1 97.5
BBB 2 98.5
EEE 5 100.0

(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();
}
}

You might also like