我的代码是基本的文件创建者和管理者。我组织所有文件的主要方式是在目录文本文件中。我将它导入一个数组,以便程序可以访问它。我的删除按钮不会从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();
}
答案 0 :(得分:0)
也许您在调试时打开了文件。确保程序可以访问它