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

标签 java file java.util.scanner bufferedreader bufferedwriter

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

除了重命名之外,一切都按预期进行。 temp.txt 文件仍然存在,groups.txt 文件未更改。我检查了 boolean 成功,它总是返回 false。有什么想法可以解决这个问题吗?

 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 文件的地方。因此,每次用户添加组时,都会通过将新的 groupName 添加到文件末尾来更新 groups.txt 文件。首先,我使用 Scanner 确保 groupName 尚不存在。 myClient 是一个 BufferedReader,它从另一个类中读取,该类存储用户在命令行中键入的内容。

最佳答案

另外,不要忘记关闭扫描仪。首先,您应该使 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:

关于java - 从另一个txt文件复制内容后如何使用Java重命名txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357349/

相关文章:

C 在字符串中搜索单词

java - 从用户获取的输入总是少于 1

java - 将数百万个 xml 文件插入 basex

java - 我应该如何为我的系统建模 - 数据库含义

c# - 如何将大文本文件拆分为较小的文件?

android - 如何在android中写入/读取数组到文件

java - 是否可以在字符串值 Java 控制台之间创建输入扫描器?

java - 判断字符串是否是 Pangram 的代码?

java - 如何初始化静态 SparseArray

java - 在 jMock 中捕获方法参数以传递给 stub 实现