Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我不能删除或重命名该文件吗?_Java_File_Writer_Reader - Fatal编程技术网

Java 为什么可以';我不能删除或重命名该文件吗?

Java 为什么可以';我不能删除或重命名该文件吗?,java,file,writer,reader,Java,File,Writer,Reader,在为类创建方法时,我遇到了一个意想不到的问题。我尝试过其他领域的解决方案,但它们对我不起作用。我的方法应该只是找到指定的行,复制文件跳过不必要的行,删除原始文件并将临时文件重命名为原始文件的名称。它成功地按预期创建了新文件,但由于无法将临时文件重命名为原始文件,因此无法删除上一个文件。我不明白,为什么 void lineDelete(String file_name, String line_to_erase){ try { int line_number = 0;

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

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);
    }
请尝试以下代码:

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);
}

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

如果使用
Path
文件.delete(Path)
文件.move(Path,Path,CopyOption…)
方法,如果操作失败,它们将抛出异常。异常名称和消息应该为您提供关于实际出错的线索

javadoc是和



1-这里有几个猜测:1)文件已在其他位置打开,因此被锁定。2) 您没有删除该文件的权限。

是否有任何异常?发布的代码甚至无法编译。未定义临时文件。发布实际代码,这确实有问题。使用Files.delete和Files.move:您将看到解释问题所在的执行选项。不,我没有收到任何异常,对tempFile感到抱歉,忘记将其更改为temp。我编辑了原始代码,以便使变量更清晰。您的项目位于哪里?工作区的位置?C:/users/user/sublime text/java/,但这是一个项目,需要在任何目录下运行。因此,在将它移动到任何目录并编译之后,它应该运行得很平稳。在我运行该方法之前,文件_name.txt已经存在,并且它包含几行,这会改变什么吗?temp.renameTo()是一个文件类型方法,不能在其中使用“.txt”之类的字符串。否。问题是,删除前一个文件后,只需调用一个方法将文件重命名为另一个文件的名称。既然你说你的逻辑是正确的,并且临时文件得到了你想要的输出,那么你唯一需要做的就是我在我的回复中发布的内容实际上,我不会像你那样做。如果您提供的文件名已经存在,您应该使用file.open,然后检查它是否存在,如果存在,是否执行您的逻辑我以前从未使用过file.open,您可以简单地解释一下吗?@janksławiński-没有
file.open
file.open