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

时间:2017-02-21 01:56:18

标签: java file java.util.scanner bufferedreader bufferedwriter

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

除重命名外,一切都按预期工作。 temp.txt文件仍然存在,groups.txt文件未更改。我检查了布尔成功,它总是返回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,它从另一个类中读取,该类存储用户在命令行中键入的内容。

1 个答案:

答案 0 :(得分:1)

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

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

有一件事可能不相关,也检查你的环境,因为

  

在Unix&#es; O / S中,您不能跨文件系统重命名To()。这种行为不同于Unix" mv"命令。当跨越文件系统时,mv会进行复制和删除,如果是这种情况,则必须执行此操作。如果您尝试将其重命名为另一个驱动器,即C: - >,则会在Windows上发生同样的情况。 d: