Как удалить запись из файла произвольного доступа? - PullRequest
0 голосов
/ 13 марта 2011

Мне было интересно, как я могу удалить запись из моего файла произвольного доступа.

Вот как я добавляю в свой 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");
    }
}

Ответы [ 2 ]

2 голосов
/ 13 марта 2011

У меня был бы удаленный флаг, и если он установлен, не читайте его.

0 голосов
/ 13 января 2014

Лучший способ: создать метод удаления диалога.Пусть этот метод запишет пустую запись в файл.Создать пустую запись легко, если ваш конструктор (ы) настроен правильно.

Когда пустая запись записывается в виде серии нулей и нулей, и если ваша программа настроена на показ только записей с правильными значениями, она не будет отображать записи с нулевыми значениями.

...