Page 367 - Cs_withBlue_J_C11_Flipbook
P. 367

Program 4      Define a class called Search to perform read and write operations on a binary file “ComputerSc.
                                 dat”. The details of the class are given as follows:
                                 Data Members
                                 int appno              :  To store application number
                                 String name            :  To store name of the student
                                 String avg             :  To store best of 4 average marks
                                 Member Methods
                                 void createFile()      :   Accepts  roll  number,  name and  average marks  of students
                                                           selected for admission  in  Computer Science Hons  and stores
                                                           them in binary file “ComputerSc.dat”
                                 void find(int ap)      :   Reads binary file “ComputerSc.dat” and  checks if application
                                                           number ‘ap’  is present in the file or not.
                                 static void main()     :  Creates object and executes other methods
                   1      import java.io.*;

                   2      import java.util.*;
                   3      class Search

                   4      {
                   5          int appno;

                   6          String name;
                   7          double avg;

                   8          void createFile() throws IOException
                   9          {

                  10              // declaring the classes required to create file
                  11              // parameter true opens file in append mode

                  12              FileOutputStream fo=new FileOutputStream("ComputerSc.dat",true);
                  13              DataOutputStream dou=new DataOutputStream(fo);

                  14              Scanner sc=new Scanner(System.in);
                  15              char ch='y';

                  16              while(ch=='y' || ch=='Y')   // loop to store multiple records
                  17              {

                  18                  System.out.println("Enter application no, name, ans average marks");
                  19                  // entering data using Scanner class

                  20                  appno=sc.nextInt();
                  21                  name=sc.next();
                  22                  avg=sc.nextDouble();

                  23                  // writing data to buffer using specific  write methods of data type






                                                                                                                       365
                                                                                 Basic Input/Output and Data File Handling    365
   362   363   364   365   366   367   368   369   370   371   372