Search code examples
javafilerandomaccessfile

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:

 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?


Solution

  • 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. 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.