【问题标题】:How do I delete a record from my random access file?如何从我的随机访问文件中删除记录?
【发布时间】:2011-03-13 18:21:44
【问题描述】:

我想知道如何从我的随机访问文件中删除一条记录。

这是我添加到我的 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");
    }
}

【问题讨论】:

  • 为什么不直接使用数据库呢?如果您需要将数据保存在单个可移植文件中,可以使用sqlite
  • 这是一个学校项目,所以我必须符合标准:(
  • 您是否希望删除此问题?你能解释一下你最近的编辑吗?

标签: java random-access


【解决方案1】:

我会有一个已删除的标志,如果设置了,请不要读入。

【讨论】:

  • 将第一个字段设置为“非法”值,例如“[[deleted]]”,这不太可能是任何人的名字。
  • 或者甚至添加一个名为 deleted 的布尔标志并将其设置为 true 或 false。
  • 找个位置,把所有的东西都调高,会有多难?
【解决方案2】:

最佳方式:创建删除对话框方法。让该方法将空白记录写入文件。如果您的构造函数设置正确,则创建空白记录很容易。

当空白记录被写入一系列零和空值时,如果您的程序设置为仅显示具有正确值的记录,它将不会显示任何具有 0/空值的记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多