Page 365 - Cs_withBlue_J_C11_Flipbook
P. 365

26                  dou.writeInt(roll);
                  27                  dou.writeUTF(name);

                  28                  dou.writeDouble(eng);
                  29                  dou.writeDouble(maths);

                  30                  dou.writeDouble(sci);
                  31                  System.out.print("Continue y/n");

                  32                  ch=sc.next().charAt(0);
                  33              }
                  34              // closing the objects transfers data in buffer to file permanently

                  35              dou.close();

                  36              fo.close();
                  37          }
                  38          void findAvg() throws IOException

                  39          {
                  40              // declaring the classes required to read file

                  41              FileInputStream fi=new FileInputStream("Marks.Dat");
                  42              DataInputStream di=new DataInputStream(fi);

                  43              boolean eof=false;
                  44              double avg;

                  45              System.out.println("Roll\tName\t\tAverage"); // printing heading
                  46              System.out.println("---------------------------------------");


                  47              //  try catch block throws End of File exception when the file is not found
                                       in directory
                  48              // or end of file is encountered
                  49              try

                  50              {

                  51                  while(!eof)
                  52                  {
                  53                      roll=di.readInt();

                  54                      name=di.readUTF();
                  55                      eng=di.readDouble();

                  56                      maths=di.readDouble();
                  57                      sci=di.readDouble();

                  58                      avg=(eng+maths+sci)/3;






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