java - 如何从随机访问文件中删除记录?

标签 java random-access

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

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

最佳答案

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

关于java - 如何从随机访问文件中删除记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5291408/

相关文章:

java - 使用 GRPC 时 SSL 解密错误

java - 在解压缩 .gz 文件时,解压缩并创建为本地副本的文件没有 .gz 可用的原始文件的最后修改时间

Java——画一个三角形

c - 快速随机访问链表节点

android - 我如何读取连接到 Android 设备的 USB 闪存的第一个扇区?

java - 在模型类中使用 java.awt.Point - 糟糕的编码风格?

java - 发生 FileNotException 时如何停止生产者线程和消费者线程

用于按需加载元素的容器的 C++ 随机访问迭代器

c++ - C++中的随机访问文件

java - RandomAccessFile - 返回空字符串