简体   繁体   English

删除Java中的数据随机访问文件

[英]Delete data random access file in Java

I'm creating a phonebook, which stores contact information and an array of phones of every contact. 我正在创建一个电话簿,其中存储联系人信息和每个联系人的电话阵列。 I'm using a randomaccessfile, mi function to store the data is: 我正在使用randomaccessfile,mi函数来存储数据是:

 public void writeRand(String name, People other, int tam) {
        RandomAccessFile file;
        try {
            file= new RandomAccessFile(name, "rw");
            file.seek(file.length());
            file.writeInt(other.getCount());
            file.writeUTF(other.getName());
            file.writeUTF(other.getAddress());
            file.writeUTF(other.getMail());
            archivo.writeUTF(other.getDate());
            for (int i = 0; i < tam; i++) {
                file.writeUTF(other.getPhones()[i]);
                file.writeBoolean(other.getPT()[i]);
                file.writeBoolean(other.getMF()[i]);
            }
            file.writeUTF(other.getNotes());
            file.writeUTF(other.getState());
            file.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(file.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(file.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

That is the structure that I use to store data for each contact, I should mention that the arrays that are stored can have different dimensions in each contact. 那就是我用来存储每个联系人数据的结构,我应该提到存储的数组在每个联系人中可以具有不同的维度。

The real question is how to create a function that allows me to delete a entire contact? 真正的问题是如何创建允许我删除整个联系人的功能?

You should be separating each contact per line. 您应该每行分隔每个联系人。 You can use NIO to get a line (which would be a contact) and then to remove the contact, remove the line. 您可以使用NIO来获得一条线路(这将是一个联系人),然后要删除该联系人,请删除该行。 There are two different ways I would use to get the contents of your file: 我将使用两种不同的方法来获取文件的内容:

String filecontents = new String( java.nio.file.Files.readAllBytes(Paths.get(new File(name).toURI)));
List<String> filecontents = java.nio.file.Files.readAllLines(Path.get(new File(name).toURI));

Using this you then have two options. 然后,您将有两个选择。

    String filecontents = new String( java.nio.file.Files.readAllBytes(Paths.get(new File(name).toURI())));
    String contactinfostart = String.valueOf(other.getCount()); //First value in file
    String contactinfoend = other.getState();
    int contactstartindex = filecontents.indexOf(contactinfostart);
    int contactendindex = filecontents.indexOf(contactinfoend + other.getState().length());
    String prefix = filecontents.substring(contactstartindex);
    String ending = filecontents.substring(contactendindex);
    String newfilecontents = prefix + ending;
    rw.seek(0);
    rw.writeUTF(newfilecontents);

Or using the string list option: 或使用字符串列表选项:

List<String> filecontentslist = java.nio.file.Files.readAllLines(Paths.get(new File(name).toURI()));
    String filecontents;
    for(int i = 0; i < filecontentslist.size(); i++){
        filecontents = filecontents + filecontentslist.get(i);
    }
//Here below is similar to first answer
    String contactinfostart = String.valueOf(other.getCount()); //First value in file
    String contactinfoend = other.getState();
    int contactstartindex = filecontents.indexOf(contactinfostart);
    int contactendindex = filecontents.indexOf(contactinfoend + other.getState().length());
    String prefix = filecontents.substring(contactstartindex);
    String ending = filecontents.substring(contactendindex);
    String newfilecontents = prefix + ending;
    rw.seek(0);
    rw.writeUTF(newfilecontents);

Either should work. 两者都应该起作用。 Note: Both write the new string to the file. 注意:两者都将新字符串写入文件。 You can remove the rw.seek and rw.writeUTF methods at the end of my code. 您可以在代码末尾删除rw.seek和rw.writeUTF方法。

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

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