Java 为什么我的程序不能从ArrayList中删除字符串

Java 为什么我的程序不能从ArrayList中删除字符串,java,arraylist,Java,Arraylist,我的代码是一个基本的文件创建者和管理者。我组织所有文件的主要方式是在一个目录文本文件中。我将它导入一个数组,以便程序可以访问它。“我的删除”按钮不会从ArrayList中删除字符串。我怎样才能解决这个问题 //This part initialises the code and imports the array private void startActionPerformed(java.awt.event.ActionEvent evt) { ArrayList<Strin

我的代码是一个基本的文件创建者和管理者。我组织所有文件的主要方式是在一个目录文本文件中。我将它导入一个数组,以便程序可以访问它。“我的删除”按钮不会从ArrayList中删除字符串。我怎样才能解决这个问题

//This part initialises the code and imports the array
private void startActionPerformed(java.awt.event.ActionEvent evt) {  
    ArrayList<String> directory = new ArrayList();
    String content1;

    try {
        content1 = new String(Files.readAllBytes(Paths.get("directory.txt")));

        output.setText(content1);
        directory.add(content1);

        refresh();
    } catch (IOException ex) {
        Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

//This allows me to refresh the output text area
private void refresh() {
    String content = "";

    for (int i = 0; i < directory.size(); i++) {
        content = content + directory.get(i) + "\n"; 
    } 
    try {
        Files.write(Paths.get("directory.txt"), directory);
    } catch (IOException ex) {
        Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    output.setText(content);
    System.out.println(directory);
}

//This deletes the file from the directory and the actual file
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                       
    directory.remove(input.getText());
    String fileDelete = input.getText();
    directory.remove(input.getText());
    Path deletefile = (Paths.get(fileDelete + ".txt"));

    try {
        Files.delete(deletefile);
    } catch (IOException ex) {
        Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        Files.write(Paths.get("directory.txt"), directory);
    } catch (IOException ex) {
        Logger.getLogger(filemanagerUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    refresh();
}           

也许您在调试文件时打开了它。确保程序可以访问它

您确实定义了ArrayList directory=new ArrayList;在方法startActionPerformed的范围内。因此,只有这个本地ArrayList会保存值,但由于这些值永远不会填充到字段目录中,而字段目录在那里的某个地方定义,所以您的口述将始终不起任何作用。我也在想同样的事情。您确定没有范围问题吗?ArrayList目录=新建ArrayList;在开始之前。我把它抄错了。