尝试从文本文件中删除特定字符串

时间:2014-03-05 14:56:44

标签: java string text file-io

我在使这个功能起作用时遇到了一些麻烦,所以在这里提供上下文。所有这些,是一个从3个不同的文本文件中删除一行的按钮。它通过从主GUI中的textFields抓取它们来确定要删除的字符串,然后搜索相关文件以查找字符串。找到字符串后,它们将被写入临时文件并刷新。

此代码确实有效但仅适用于第一个文件。我对这段代码唯一的问题是,最后一次重命名不会因某些原因而起作用吗?我需要将文件重命名回原件,以便我的软件的其余部分能够正确运行。

请有人帮忙吗? :)

removeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    // TODO
                    String name = reminderNameField.getText();
                    String date = reminderDate.getText();
                    String details = reminderDetailsField.getText();

                    File fileName = new File("reminderNames.txt");
                    File fileDate = new File("reminderDate.txt");
                    File fileDetails = new File("reminderDetails.txt");


                    try {
                    File tempFileN = new File(fileName.getAbsoluteFile() + ".tmp");
                    File tempFileD = new File(fileDate.getAbsoluteFile() + ".tmp");
                    File tempFileC = new File(fileDetails.getAbsoluteFile() + ".tmp");

                    BufferedReader brName = new BufferedReader(new FileReader("reminderNames.txt"));
                    BufferedReader brDate = new BufferedReader(new FileReader("reminderDate.txt"));
                    BufferedReader brDetails = new BufferedReader(new FileReader("reminderDetails.txt"));

                    PrintWriter pwName = new PrintWriter(new FileWriter(tempFileN));
                    PrintWriter pwDate = new PrintWriter(new FileWriter(tempFileD));
                    PrintWriter pwDetails = new PrintWriter(new FileWriter(tempFileC));

                    String lineN = null;
                    String lineD = null;
                    String lineC = null;
                    while ((lineN = brName.readLine()) !=null && (lineD = brDate.readLine()) !=null && (lineC = brDetails.readLine()) !=null) {
                        if(!lineN.trim().equals(name) && !lineD.trim().equals(date) && !lineC.trim().equals(details)) {
                            pwName.println(lineN);
                            pwName.flush();
                            pwDate.println(lineD);
                            pwDate.flush();
                            pwDetails.println(lineC);
                            pwDetails.flush();
                        }
                    }

                    pwName.close();
                    pwDate.close();
                    pwDetails.close();

                    brName.close();
                    brDate.close();
                    brDetails.close();

                    fileName.delete();
                    fileDate.delete();
                    fileDetails.delete();

                    if(!tempFileN.renameTo(fileName)) {
                        System.out.println("Cannot rename file");
                    }
                    if(!tempFileD.renameTo(fileDate)) {
                        System.out.println("Cannot rename file date");
                    }
                    if(!tempFileC.renameTo(fileDetails)) {
                        System.out.println("Cannot rename file details");
                    }

*更新* 写这个方法确实有效,但由于某种原因它只适用于一个文件?谁能告诉我为什么?

public void removeReminder(File a, String search) throws IOException {

File tempFile = new File(a.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(a));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;

while ((line = br.readLine()) != null) {
    if (!line.trim().equals(search)) {
        pw.println(line);
        pw.flush();
    }
}
pw.close();
br.close();

a.delete();

tempFile.renameTo(a);

}

1 个答案:

答案 0 :(得分:0)

尝试使用以下方式阅读文件中的文字:

 public static String readAllText(String filename) throws Exception {
    StringBuilder sb = new StringBuilder();
    Files.lines(Paths.get(filename)).forEach(sb::append);
    return sb.toString();
}

然后你可以删除或更改.replace:

String file1 = readAllText("temp.tmp"); file1.replace(fileName,"");