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

删除Java中的数据随机访问文件,java,file,randomaccessfile,Java,File,Randomaccessfile,我正在创建一个电话簿,它存储联系人信息和每个联系人的一系列电话。我正在使用randomaccessfile,mi函数来存储数据: public void writeRand(String name, People other, int tam) { RandomAccessFile file; try { file= new RandomAccessFile(name, "rw"); file.seek(file.

我正在创建一个电话簿,它存储联系人信息和每个联系人的一系列电话。我正在使用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);
        }
    }
public void writeRand(字符串名、其他人、int-tam){
随机存取文件;
试一试{
文件=新的随机访问文件(名称,“rw”);
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
这就是我用来存储每个联系人数据的结构,我应该提到,存储的数组在每个联系人中可以有不同的维度


真正的问题是如何创建一个允许我删除整个联系人的函数?

您应该将每行的每个联系人分开。您可以使用NIO获取一条线路(这将是一个触点),然后要删除触点,请删除该线路。我将使用两种不同的方式获取文件的内容:

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));
或使用字符串列表选项:

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);
List filecontentslist=java.nio.file.Files.readAllLines(path.get(新文件(名称).toURI());
字符串文件内容;
对于(int i=0;i

两者都应该有效。注意:两者都将新字符串写入文件。您可以在代码末尾删除rw.seek和rw.writeUTF方法。

您应该使用数据库和SQL解决此类问题。
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);