Dps International School: Subject: Computer Applications Topic: File Handling Name: Grade:11 ISC
Dps International School: Subject: Computer Applications Topic: File Handling Name: Grade:11 ISC
Name:
Grade :11 ISC Prepared by : Ms. CHANDNI MATHUR
Reader, InputStreamReader, FileReader and BufferedReader
Reader is the abstract class for reading character streams. It implements the following
fundamental methods:
import java.io.*;
public class ReadingFromFile
{
public static void main(String[] args) throws Exception
{
try {
FileReader reader =new FileReader("C:\\Users\\COM310\\Desktop\\test1.txt");
int character;
} catch (IOException e) {
}
}
}
DPS INTERNATIONAL SCHOOL
try {
FileWriter writer = new FileWriter("C:\\Users\\COM310\\Desktop\\test.txt", true);
writer.write("Hello World");
writer.write("\n"); // write new line
writer.write("Good Bye!");
writer.close();
} catch (IOException e) {
//e.printStackTrace();
}
}
import java.io.*;
class FileWrite
{
public static void main(String args[])throws IOException
{
FileWriter fw = new FileWriter("C:\\Users\\Chandni\\Desktop\\fruits.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
pw.println("Apple");
pw.println("Grapes");
pw.println("kiwi");
pw.println("orange");
pw.close();
}
}
DPS INTERNATIONAL SCHOOL
// Java Program to illustrate read contents already present from fruits.txt
import java.io.*;
class FileRead
{
public static void main(String args[])throws IOException
{
FileReader fr = new FileReader("C:\\Users\\Chandni\\Desktop\\fruits.txt");
BufferedReader br = new BufferedReader(fr);
String n;
while((n=br.readLine())!=null)
{
System.out.println(n);
}
}
}
//Java program to write your name and marks into a binary or data file(abc.dat)
import java.io.*;
class DataFileWrite
{
public static void main(String args[])throws IOException
{
FileOutputStream fos = new FileOutputStream("C:\\Users\\Chandni\\Desktop\\abc.dat");
DataOutputStream dos= new DataOutputStream(fos);
dos.writeUTF("Ajay");
dos.writeInt(5);
}
}
DPS INTERNATIONAL SCHOOL
import java.io.*;
class ReadData
{
public static void main(String args[])throws IOException
{
FileInputStream fis = new FileInputStream("C:\\Users\\Chandni\\Desktop\\abc.dat");
DataInputStream dis= new DataInputStream(fis);
boolean eof= false;
while(!eof)
{
try
{
String n= dis.readUTF();
int m= dis.readInt();
System.out.println("name= "+ n + "\n Marks= " + m);
}
catch(EOFException e)
{
eof=true;
}
}
}
}
DPS INTERNATIONAL SCHOOL
Question:
A binary file named “ABC.DAT” contains the product code (pc), unit price (up) and
quantity(q) for number of items.
Write a Method to accept a product code ‘p’ and check the availability of the product
and display with an appropriate message.
Solution:
if (p==pc)
{
System.out.println(“Product Found”);
System.out.println(“product Code =” + pc);
System.out.println(“Unit Price =” + up);
System.out.println(“Quantity =” + q);
flag=1;
}
}
catch(EOFException e)
{
eof=true;
}
if( flag==0)
System.out.println(“Product Not Found”);
}
dis.close();
fis.close();
}
DPS INTERNATIONAL SCHOOL