简体   繁体   English

使用RandomAccessFile删除记录

[英]Delete a record using RandomAccessFile

what i currently able to do for deleting a record from my .txt file is to put all the characters as empty but unfortunately it just make make those unit as empty and it will not remove those units. 我目前能够从.txt文件中删除记录的功能是将所有字符都设置为空,但是不幸的是,它只是使这些单元为空,并且不会删除这些单元。 do you have any idea how can i completely delete a row of record from my txt file without rewriting the whole file again ? 您有什么主意,如何可以完全从我的txt文件中删除一行记录,而无需再次重写整个文件?

protected boolean DeleteObject(int mID)
{
    try {
        file = new RandomAccessFile(new File("UserDB.txt"), "rw");
        long FileSize = file.length();
        file.seek(0);
        long NUMRecords = FileSize / RECORD;
        for (int j=0; j < NUMRecords; j++) {

            sID = file.readUTF();
            for (int i = 0; i < 10 - sID.length(); i++) {
                file.readByte();
            }
            ID = Integer.parseInt(sID);

            fName = file.readUTF();
            for (int i = 0; i < 20 - fName.length(); i++) {
                file.readByte();
            }

            surname = file.readUTF();
            for (int i = 0; i < 20 - surname.length(); i++) {
                file.readByte();
            }

            sMajor = file.readUTF();
            for(int i=0; i<3-sMajor.length(); i++){
                file.readByte();
            }
            major = Integer.parseInt(sMajor);

            if(ID == mID)
            {
                file.seek(j*RECORD);
                for(int k =0; k<RECORD; k++)
                {
                    file.writeUTF("");
                }
                return true;
            }
        }
    }
    catch (IOException e) {
        System.out.println("ERROR IN DELETING FILE "+e);
        e.getStackTrace();
    }

    System.out.println("Nothing Found");
    return false;
}

You could design your file to contain a logical delete flag. 您可以将文件设计为包含逻辑删除标志。 When deleting the record you would then update the logical delete flag, marking the record as deleted rather than physically removing it from the file. 删除记录时,您将更新逻辑删除标志,将记录标记为已删除,而不是将其从文件中物理删除。

I once had a file intended as a queue. 我曾经有一个文件打算作为队列。 The concept is that deleting always occurred linearly since it is a queue. 概念是删除总是线性发生的,因为它是一个队列。 So the deleting process would just rewrite the file offset of the next non-deleted row. 因此,删除过程将只重写下一个未删除行的文件偏移量。

I reserved the first 8 bytes of the file as a file pointer (whose value was 8 or above). 我将文件的前8个字节保留为文件指针(其值等于或大于8)。 This makes the file binary though, not txt anymore. 但这使文件变成二进制文件,不再是txt文件。 You could write that offset in a sibling file though. 您可以将该偏移量写入同级文件中。

However, this being a queue, I had to create new files from time to time and garbecollect the entirely deleted ones. 但是,由于这是一个队列,我不得不不时创建新文件并收集完全删除的文件。

Otherwise, no you cannot cut away a file portion. 否则,不能,您不能切掉文件部分。 Each row must have it's own delete flag. 每行必须具有自己的删除标志。 And vacuuming/packing/managing free space (or rewriting a clean version) is a sad reality for every database out there. 清理/打包/管理可用空间(或重写干净的版本)对于那里的每个数据库都是可悲的现实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM