الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

اريد ان اعرف نهاية الفايل في الجافا

مغلق
بدأه تالا في 28 أبريل 2005 · 29 رد · 2,326 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#26

شكرا لكم

سأجرب الطريقتين لأن المشكلة لم تحل الى الان

#27

السلام عليكم

لم اجرب ما اقترحته اخ ابو مازن فهل انت جربت ان تضيف إلى ملف append واستطعت قراءة البيانات التي اضفتها للملف بشكل سليم دون رمي الاستثناء CorruptedFileException باستعمال الطريقة التي اقترحتها

#28

نعم...

#29

السلام عليكم

جربت طرقة الاخ ابو مازن وهذا هو كود الاضافة :

public void addRecord()
   {
      int studNumber = 0;
      studrec record;
      String fieldValues[] = userInterface.getFieldValues();
      
      if ( ! (fieldValues[ userInterface.NAME ].equals( "" ) ) ){
              
         // output values to file
         try {
              openFile(file);
                studNumber = Integer.parseInt(fieldValues[ userInterface.ID ]);

                
    if ( studNumber > 0 )
       {
               // create new record
                record = new studrec(fieldValues[studenterUI.NAME],
                                     studNumber,0);
                // output record and flush buffer
                output.writeObject( record );
                output.flush();
                closeFile();
                }//if
             else
             {
                 JOptionPane.showMessageDialog( this,
                    "Student number must be greater than 0",
                    "Bad Student number", JOptionPane.ERROR_MESSAGE );
             }

            // clear textfields
            userInterface.clearFields();
            //closeFile();
         } // end try

         // process invalid account number or balance format
         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad Student number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }

         // process exceptions from file output
         catch ( IOException ioException ) {
             JOptionPane.showMessageDialog( this, "Error writing to file",
                "IO Exception", JOptionPane.ERROR_MESSAGE );
            System.out.println("closing file");
            //closeFile();
         }
   } 
   }// end method addRecord

وهذا كود القراءة :

public String[] readRecord()
   {
      studrec record=null;
      // input the values from the file
      try {
         
         record = ( studrec ) input.readObject();
        
         // create array of Strings to display in GUI
         String values[] = { String.valueOf( record.getstudentid()),
            record.getstudentname(),String.valueOf( record.getGrade())};
         System.out.println(" Name =" + record.getstudentname());
         
         fin.skip((long)4);
         return values;

       
          }

      // display message when end-of-file reached
      catch ( EOFException eofe ) {

         //System.out.println("Error: "+record);
         
       JOptionPane.showMessageDialog( this, 
      "End of file exception read" + eofe,
          "EOFException", JOptionPane.ERROR_MESSAGE );
          eofe.printStackTrace();
         return null;
     
    }

      // display error message if class is not found
       catch ( ClassNotFoundException cnfe ) {
       
         System.out.println("Error: "+record);
         
       
       JOptionPane.showMessageDialog( this, 
          "Unable to create object" + cnfe,
          "ClassNotFoundException", JOptionPane.ERROR_MESSAGE );
         cnfe.printStackTrace();
       return null;
    }

      // display error message if cannot read due to problem with file
     catch (IOException ioe ) {

         System.out.println("Error: "+record);
         
       JOptionPane.showMessageDialog( this, 
          "Error during read from file" + ioe,
          "IOException", JOptionPane.ERROR_MESSAGE );
        ioe.printStackTrace();
     return null;
    }
    catch (Exception e ) {

         System.out.println("Error: "+record);
         
       JOptionPane.showMessageDialog( this, 
          "Error during read from file" + e,
          "Exception", JOptionPane.ERROR_MESSAGE );
       e.printStackTrace();
      return null;
    }

   } // end method readRecord

جزاكم الله خير

والبروجكت الان شارف على الانتهاء

#30

طيب، لم تقل لنا هل نفعت الطريقة أم لا؟!

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…