Page 380 - Cs_withBlue_J_C11_Flipbook
P. 380

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);
                378378  Touchpad Computer Science-XI
   375   376   377   378   379   380   381   382   383   384   385