Page 354 - computer science (868) class 11
P. 354

dout.writeInt(productC);
                                    dout.writeUTF(productN);
                                    dout.writeDouble(price);
                                    dout.writeInt(quantity);
                                    dout.writeInt(reorder);
                                }
                            }
                            catch(EOFException e)
                            {
                                System.out.println("=======EOF=========");
                                eof=true;
                            }
                            din.close();fin.close();
                            dout.close();fout.close();
                            // Deleting Product.dat and renaming Newproduct.dat as Product.dat
                            File f1=new File("Product.dat");
                            f1.delete();
                            File f2=new File("Newproduct.dat");
                            boolean b=f2.renameTo(f1);
                            if(b)
                                System.out.println("File renamed successfully");
                            else
                                System.out.println("File error");
                        }
                        void delRec() throws IOException
                        {
                            /*Original file opened in read mode and new file in write mode
                            All records other than the record to be deleted will be written
                            to the new file. Original file is deleted and the new file is
                            renamed to previous original file*/
                            FileOutputStream fout=new FileOutputStream("Newproduct.dat");
                            DataOutputStream dout=new DataOutputStream(fout);
                            FileInputStream fin=new FileInputStream("Product.dat");
                            DataInputStream din=new DataInputStream(fin);
                            int ino;
                            System.out.println("Enter Product code to delete");
                            ino=sc.nextInt();
                            boolean eof=false;
                            try
                            {
                                while(!eof)
                                {
                                    productC=din.readInt();
                                    productN=din.readUTF();
                                    price=din.readDouble();
                                    quantity=din.readInt();
                                    reorder=din.readInt();





                                     //If product code does not match then the records are written to Newproduct.dat

                                    if(productC!=ino)
                                    {
                                        dout.writeInt(productC);
                352352  Touchpad Computer Science-XI
   349   350   351   352   353   354   355   356   357   358   359