DPS INTERNATIONAL SCHOOL
SUBJECT: COMPUTER APPLICATIONS
                              TOPIC: FILE HANDLING
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:
          read(): reads a single character.
          read(char[]): reads an array of characters.
          skip(long): skips some characters.
          close(): closes the stream.
InputStreamReader is a bridge from byte streams to character streams. It converts bytes
into characters using a specified charset. The charset can be default character encoding of the
operating system, or can be specified explicitly when creating an InputStreamReader.
FileReader is a convenient class for reading text files using the default character encoding of
the operating system.
BufferedReader reads text from a character stream with efficiency (characters are buffered
to avoid frequently reading from the underlying stream) and provides a convenient method
for reading a line of text readLine().
The following diagram show relationship of these reader classes in the java.io package:
2. Writer, OutputStreamWriter, FileWriter and BufferedWriter
Writer is the abstract class for writing character streams. It implements the following
fundamental methods:
          write(int): writes a single character.
          write(char[]): writes an array of characters.
          write(String): writes a string.
          close(): closes the stream.
OutputStreamWriter is a bridge from byte streams to character streams. Characters are
encoded into bytes using a specified charset. The charset can be default character encoding of
the operating system, or can be specified explicitly when creating an OutputStreamWriter.
                           DPS INTERNATIONAL SCHOOL
FileWriter is a convenient class for writing text files using the default character encoding of
the operating system.
BufferedWriter writes text to a character stream with efficiency (characters, arrays and
strings are buffered to avoid frequently writing to the underlying stream) and provides a
convenient method for writing a line separator: newLine().
The following diagram show relationship of these writer classes in the java.io package:
 // Java Program to illustrate reading fromFileReader using FileReader
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;
        while ((character = reader.read()) != -1) {
System.out.print((char) character);
        }
reader.close();
        } catch (IOException e) {
    }
}
}
                        DPS INTERNATIONAL SCHOOL
 // Java Program to illustrate reading from FileWriter using FileWriter
import java.io.*;
public class WritingToFile {
    public static void main(String[] args) {
        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();
      }
      }
 // Java Program to illustrate write fruits name in text file(fruits.txt)
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
//Java program to read from the data file (abc.dat)
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.
The method declaration is as follows:
             void findpro( int p )
Solution:
void findPro(int p) throws IOException)
{
      FileInputStream fis = new FileInputStream("C:\\Users\\Chandni\\Desktop\\abc.dat");
     DataInputStream dis= new DataInputStream(fis);
      boolean eof= false;
      int flag=0;
     while(!eof)
     {
        try
        {
           int pc= dis.readInt();
           int up= dis.readInt();
           int q= dis.readInt();
               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