2014-03-05 128 views
0

我在获取该函数时遇到了一些麻烦,所以在这里提供了上下文的升技。所有这一切,都是一个从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"); 
        } 

* UPDATE * 写这个方法中做的工作,但由于某种原因,它仅适用于一个文件?谁能告诉我为什么?

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); 

}

+2

我很困惑。为什么你没有一个函数可以一次处理一个文件,而只是遍历这三个文件? –

+0

我开始认为这会很容易,但一直试图让它工作的麻烦负荷?你会如何推荐这样做? –

+0

编写一个文件的方法。确保首先工作。一旦这样做,很可能(a)你的问题会神奇地消失,或者(b)问题会神奇地变得明显。 –

回答

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,"");