有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将文件重命名为现有的java文件

我正在努力

  1. 创建临时文件“Temp Account Info.txt”
  2. 将现有帐户文件“Account information.txt”中的信息写入“Temp Account Info.txt”
  3. 省略某些信息
  4. 删除“Account Information.txt”
  5. 将“Temp Account Info.txt”重命名为“Account Information.txt”

我的问题是第4步和第5步。我也不确定我点的菜是否正确。 代码如下所示

public static void deleteAccount(String accountNumber) throws Exception {
    File accountFile = new File("Account Information.txt");
    File tempFile = new File("Temp Account Info.txt");
    BufferedReader br = new BufferedReader(new FileReader(accountFile));
    FileWriter tempFw = new FileWriter(tempFile, true);
    PrintWriter tempPw = new PrintWriter(tempFw);

    String line;
    try {
        while ((line = br.readLine()) != null) {
            if(!line.contains(accountNumber)) {                    
                tempPw.print(line);
                tempPw.print("\r\n");
            }
        }
        **FileWriter fw = new FileWriter(accountFile);
        PrintWriter pw = new PrintWriter(fw);
        tempFile.renameTo(accountFile);
        accountFile.delete();
        fw.close();
        pw.close();
        tempFw.close();
        tempPw.close();
    } catch (Exception e) {
        System.out.println("ERROR: Account Not Found!");
    }
}

完整的代码可以在:https://hastebin.com/eposapecep.java

任何帮助都将不胜感激

我知道我没有正确地检查“帐户未找到”,并将在命名问题后尝试解决这个问题

提前谢谢


共 (0) 个答案