[英]Trying to remove specific strings from text files
我在使该功能起作用时遇到了一些麻烦,因此在这里我提供了一些背景信息。 所有这些就是一个按钮,用于从3个不同的文本文件中删除一行。 它通过从主GUI的textField中获取字符串来确定要删除的字符串,然后搜索相关文件以找到字符串。 一旦找到字符串,它们将被写入临时文件并刷新。
该代码确实有效,但仅适用于第一个文件。 我在这段代码中遇到的唯一问题是,由于某些原因,最后一个也重命名不会起作用? 我需要将文件重命名为原始文件,以便其余软件可以正常运行。
有人可以帮忙吗? :)
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);
}
尝试使用以下方法从文件中读取文本:
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,"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.