简体   繁体   English

Java 函数 delete() 和 renameTo() 不删除也不重命名

[英]Java functions delete() and renameTo() not deleting nor renaming

public void newEditSportRecord(){
    String filepath = "sport.txt"; //exists in C:\Users\Dell\Documents\NetBeansProjects\Assignment\
    String editTerm = JOptionPane.showInputDialog("Enter ID of Sport you wish to modify:");
    
    String tempFile = "temp.txt"; // to be created in C:\Users\Dell\Documents\NetBeansProjects\Assignment\
    File oldFile = new File(filepath);
    System.out.println(oldFile.getAbsolutePath()); // prints C:\Users\Dell\Documents\NetBeansProjects\Assignment\sport.txt
    File newFile = new File(tempFile);
    System.out.println(newFile.getAbsolutePath()); // prints C:\Users\Dell\Documents\NetBeansProjects\Assignment\temp.txt
    String ID, name = "";

    try {
        FileWriter fw = new FileWriter(tempFile, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        x = new Scanner(new File(filepath));
        
        while (x.hasNextLine()) {
            ID = x.next();
            System.out.println(ID);
            name = x.next();
            System.out.println(name);

            if (ID.equals(editTerm)) {
                newID = JOptionPane.showInputDialog("Enter new Sport ID:");
                newName = JOptionPane.showInputDialog("Enter new Sport Name:");
                pw.println(newID);
                pw.println(newName);
                pw.println();
                JOptionPane.showMessageDialog(modifySport, "Record Modified");
            } else {
                pw.println(ID);
                pw.println(name);
                pw.println();
            }
        }
        x.close();
        pw.flush();
        pw.close();
        oldFile.delete();
        File dump = new File(filepath);
        newFile.renameTo(dump);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(modifySport, ex);
    }
}

I have the following function to try and modify a text file.我有以下 function 来尝试修改文本文件。 However, it does NOT delete the original file "sport.txt" nor does it rename "temp.txt" to "sport.txt".但是,它不会删除原始文件“sport.txt”,也不会将“temp.txt”重命名为“sport.txt”。 It DOES read from the file and create a copy of "sport.txt" with all the relevant modifications as "temp.txt".它确实从文件中读取并创建“sport.txt”的副本,其中所有相关修改为“temp.txt”。 I had suspected it was a problem with the writers but having closed all of them, the issue still persists.我曾怀疑这是作家的问题,但在关闭所有作家后,问题仍然存在。 Is this simply down to permission problems as the folder exists in the Documents folder on Local Disk?这仅仅是权限问题,因为该文件夹存在于本地磁盘上的 Documents 文件夹中吗?

Yes, it is a permission problem.是的,这是一个权限问题。 Either change the permission of the Documents folder and give access to all the permissions to your user or change your working folder.更改 Documents 文件夹的权限并向您的用户授予对所有权限的访问权限,或者更改您的工作文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM