从另一个 txt 文件复制内容后,如何使用 Java 重命名 txt 文件

How to rename a txt file using Java after copying contents from another txt file

我创建了一个程序,其中有一个名为 groups.txt 的文件。该文件包含一个名称列表。要删除组,它必须存在于文件中。我使用 Scanner 方法在每一行中搜索名称。如果它包含该行,则将 val 设置为 1。这会触发 val == 1 条件。在此期间我想做的是尝试从 groups.txt 文件中删除 groupName。为此,我创建了一个名为 TempFile 的新 txt 文件,它复制了 groups.txt EXCEPT groupName 中的所有名称。然后将此文件重命名为 groups.txt,并删除旧的 groups.txt 文件。

除重命名外,一切正常。 temp.txt 文件仍然存在,groups.txt 文件未更改。我检查了布尔成功,它总是 returns 为假。有什么解决办法吗?

 if (method.equals("delete group")){
                int val = 0;
                String groupName = myClient.readLine();

                try {
                    Scanner file = new Scanner(new File("groups.txt"));

                    while (file.hasNextLine()){
                        String line = file.nextLine();

                        if (line.indexOf(groupName) != -1){

                                val = 1;    
                        } 
                    }
                     if (val == 1){

                                try {

                                    File groupFile = new File("groups.txt");
                                    File tempFile = new File("temp.txt");

                                    BufferedReader reader = new BufferedReader(new FileReader(groupFile));
                                    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

                                    String currentLine;
                                    System.out.println(groupName);
                                    while ((currentLine = reader.readLine()) != null){

                                        String trimLine = currentLine.trim();

                                        if (trimLine.equals(groupName)){
                                             continue;
                                        } else {
                                            writer.write(currentLine + System.getProperty("line.separator"));
                                        }
                                    }
                                    writer.close();
                                    reader.close();
                                    groupFile.delete();
                                    boolean success = tempFile.renameTo("groups.txt");

                                } catch (IOException f){
                                    System.err.println("File Not Found: " + f.getMessage());
                                }                       }

                } catch (FileNotFoundException f){
                    System.err.println("File Not Found Exception: " + f.getMessage());
                }

            }

上述之前的代码:

 if (command.equals("group")){
            String method = myClient.readLine();

            if (method.equals("create group")){

                String groupName = myClient.readLine();

                int val = 0;

                try {
                    Scanner file = new Scanner(new File("groups.txt"));

                    while (file.hasNextLine()){
                        String line = file.nextLine();
                        if (line.indexOf(groupName) != -1){
                            Report.error("group name already exists, please pick another");
                            val = 1;                                
                        }
                    }
                } catch (FileNotFoundException f){
                    System.err.println("File Not Found: " + f.getMessage());
                }

                if (val == 0){
                    try {
                        PrintWriter out = new PrintWriter(new FileWriter("groups.txt", true));
                        out.println(groupName);
                        out.close();

                    } catch (IOException e){
                        Report.error("IOException: " + e.getMessage());
                    }
                }   

在代码的第二部分,这是我最初更新 groups.txt 文件的地方。因此,每次用户添加组时,它都会通过将新组名添加到文件末尾来更新 groups.txt 文件。首先,我使用 Scanner 确保 groupName 不存在。 myClient 是一个 BufferedReader,它从另一个 class 读取,它存储用户在命令行中键入的内容。

另外不要忘记关闭扫描仪。首先你应该使 delete() 工作并确保你知道你当前的工作目录,并写下你的文件路径相对于它。检查:

File file = new File("abc.txt");
System.out.println(file.getAbsolutePath());

有一件事可能无关,还要检查你的环境,因为

In the Unix'esque O/S's you cannot renameTo() across file systems. This behavior is different than the Unix "mv" command. When crossing file systems mv does a copy and delete which is what you'll have to do if this is the case. The same thing would happen on Windows if you tried to renameTo a different drive, i.e. C: -> D: