2011-03-13 60 views
0

我想知道如何從我的隨機訪問文件中刪除記錄。如何從我的隨機訪問文件中刪除記錄?

這是我如何添加到我的RAF,不知道如何雖然刪除它:X

public void addNewStudent(String name, String formClass, String emailAddress, String country1, String country2, String universityChoice) 
{ 
    try 
    { 
     RandomAccessFile theFile = new RandomAccessFile("studentData.dat","rw"); 
     long records = (theFile.length()+299)/300; //Number of records 
     if(theFile.length()>0) //Check if the file is empty or not 
     { 
      for(long x=0;x<records;x++) 
      { 
       theFile.seek(x*300); 
       String currentName = theFile.readUTF(); 
       if(name.equalsIgnoreCase(currentName)) //Check if student exists in database 
       { 
        output("This student exist already"); //Output this if exists 
       } 
       else // or else write a new record 
       { 
        theFile.seek(records*300); 
        theFile.writeUTF(name); //Write student name 
        theFile.seek((records*300)+50); 
        theFile.writeUTF(formClass); //Writes students' form class 
        theFile.seek((records*300)+60); 
        theFile.writeUTF(emailAddress); //Writes students' email 
        theFile.seek((records*300)+100); 
        theFile.writeUTF(country1); //Writes students' country choice #1 
        theFile.seek((records*300)+140); 
        theFile.writeUTF(country2); //Writes students' country choice #2 
        theFile.seek((records*300)+180); 
        theFile.writeUTF(universityChoice); //Writes students' university choices 
        students.add(name,formClass,emailAddress,country1,country2,universityChoice); 
       } 
      } 
     } 
     else //If the file isn't empty, then just write 
     { 
      theFile.seek(records*300); 
      theFile.writeUTF(name); //Write student name 
      theFile.seek((records*300)+50); 
      theFile.writeUTF(formClass); //Writes students' form class 
      theFile.seek((records*300)+60); 
      theFile.writeUTF(emailAddress); //Writes students' email 
      theFile.seek((records*300)+100); 
      theFile.writeUTF(country1); //Writes students' country choice #1 
      theFile.seek((records*300)+140); 
      theFile.writeUTF(country2); //Writes students' country choice #2 
      theFile.seek((records*300)+180); 
      theFile.writeUTF(universityChoice); //Writes students' university choices 
      students.add(name,formClass,emailAddress,country1,country2,universityChoice);   
     } 
    } 

    catch(IOException e) 
    { 
     output("Error while adding new student"); 
    } 
} 
+0

爲什麼不只是使用數據庫?如果您需要將數據存儲在單個可移植文件中,則可以使用[sqlite](http://www.sqlite.org/)。 – 2011-03-13 18:28:47

+0

這是一所學校的項目,所以我必須要標準:( – 2011-03-13 18:30:24

+0

您尋找的這個問題被刪除內?你能解釋一下你最近編輯? – 2011-03-13 22:59:44

回答

2

我想有一個刪除標誌,如果它的設置,不讀它

+0

如何做任何提示嗎? – 2011-03-13 18:28:20

+0

設置第一場一個「非法」的值,如「[刪除]」,這是不太可能是任何人的名字。 – Mat 2011-03-13 18:31:25

+1

甚至添加一個名爲布爾標誌'deleted'並將其設置爲true或false。 – 2011-03-13 18:33:10

0

。最佳途徑:創建一個刪除對話框方法。讓該方法向該文件寫入空白記錄。如果您的構造函數設置正確,創建空白記錄很容易。

當空記錄寫成一系列零和空值的,如果你的程序設置爲只顯示記錄與正確的價值觀,它不會與0 /空值顯示任何記錄。