【问题标题】:Why can't I neither delete nor rename the file?为什么我既不能删除也不能重命名文件?
【发布时间】:2016-08-24 16:02:42
【问题描述】:

在为我的类创建方法时,我遇到了一个意想不到的问题。我尝试过其他人的解决方案,但它们对我不起作用。我的方法应该简单地找到指定的行,复制文件跳过不必要的行,删除原始文件并将临时文件重命名为原始文件的名称。它按预期成功创建了新文件,但由于无法将临时文件重命名为原始文件,因此无法删除前一个文件。我想不通,为什么?

void lineDelete(String file_name, String line_to_erase){
    try {
        int line_number = 0;
        String newline = System.getProperty("line.separator");
        File temp = new File("temporary.txt");
        File theFile = new File(file_name+".txt");
        String path = theFile.getCanonicalPath();
        File filePath = new File(path);

        BufferedReader reader = new BufferedReader(new FileReader(file_name + ".txt"));
        BufferedWriter writer = new BufferedWriter(new FileWriter(temp));

        String lineToRemove = line_to_erase;
        String currentLine;

        while((currentLine = reader.readLine()) != null) {
            String trimmedLine = currentLine.trim();
            if(trimmedLine.equals(lineToRemove)){
                continue;
            }
            writer.write(currentLine + newline));
        }
        writer.close(); 
        reader.close();
        filePath.delete();
        temp.renameTo(theFile);
    }
    catch (FileNotFoundException e){
        System.out.println(e);
    }
    catch (IOException e){
        System.out.println(e);
    }

【问题讨论】:

  • 你有什么异常吗?
  • 发布的代码甚至无法编译。 tempFile 未定义。贴出实际代码,确实有问题。使用 Files.delete 和 Files.move:你会得到解释问题所在的 execptions。
  • 不,我没有得到任何异常,并且对 tempFile 感到抱歉,忘记将其更改为 temp。我编辑了原始代码,使变量更清晰。
  • 您的项目在哪里?工作区的位置?
  • C:/users/user/sublime text/java/,但这是一个项目,它需要在任何目录中运行。所以移动到任意目录并编译后,应该可以顺利运行

标签: java file writer reader


【解决方案1】:

试试这个代码:

void lineDelete(String file_name, String line_to_erase){
try {
    int line_number = 0;
    String newline = System.getProperty("line.separator");
    File temp = new File("temporary.txt");
    File theFile = new File(file_name+".txt");
    String path = theFile.getCanonicalPath();

    BufferedReader reader = new BufferedReader(new FileReader(theFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(temp));

    String lineToRemove = line_to_erase;
    String currentLine;

    while((currentLine = reader.readLine()) != null) {
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(lineToRemove)){
            continue;
        }
        writer.write(currentLine + newline));
    }
    writer.close(); 
    reader.close();
    theFile.delete();
    temp.renameTo(file_name + ".txt");
}
catch (FileNotFoundException e){
    System.out.println(e);
}
catch (IOException e){
    System.out.println(e);
}

【讨论】:

  • 在我运行该方法之前文件 file_name.txt 已经存在并且它包含几行的事实是否改变了任何东西?而temp.renameTo()是File类型的方法,不能在里面使用“.txt”之类的字符串。
  • 没有。问题是,在删除前一个文件后,您只需调用一个方法将文件重命名为另一个文件的名称。既然你说你的逻辑没问题,并且临时文件得到你想要的输出,你唯一需要做的就是我在我的答案中发布的内容
  • 其实我不会像你那样做。如果您提供的文件名已经存在,您应该使用 File.open,然后检查它是否存在,如果存在,请执行您的逻辑
  • 我之前没用过file.open,你能简单解释一下吗?
  • @JanekSławiński - 没有 File.open 或 file.open 这样的东西。
【解决方案2】:

我可以提出删除和/或重命名可能失败的几个原因,但有一个比猜测更好的方法来解决您的问题1。

如果您使用Path 以及Files.delete(Path) 和Files.move(Path, Path, CopyOption...) 方法,如果操作失败,它们将抛出异常。异常名称和消息应该为您提供实际上出了什么问题的线索。

javadoc 是 here 和 here。


1 - 这里有几个猜测:1) 文件已在别处打开,因此被锁定。 2) 您无权删除该文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2018-01-24
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多