【问题标题】:file.delete() wont delete file, javafile.delete() 不会删除文件,java
【发布时间】:2013-12-20 04:08:00
【问题描述】:

好的,这会有点长。所以我做了一个junit测试类来测试我的程序。我想测试使用 Scanner 将文件读入程序的方法是否抛出异常,如果文件不存在,如下所示:

@Test
public void testLoadAsTextFileNotFound()
{
    File fileToDelete = new File("StoredWebPage.txt");  

    if(fileToDelete.delete()==false) {
        System.out.println("testLoadAsTextFileNotFound - failed");
        fail("Could not delete file");
    }

    try{    
        assertTrue(tester.loadAsText() == 1);
        System.out.println("testLoadAsTextFileNotFound - passed");
    } catch(AssertionError e) {
        System.out.println("testLoadAsTextFileNotFound - failed");
        fail("Did not catch Exception");

    }
}

但测试失败,“无法删除文件”,所以我做了一些搜索。路径是正确的,我有权访问该文件,因为程序首先创建了它。因此,唯一的其他选择是,进出文件的流仍在运行。所以我检查了方法,以及使用文件的另一个方法,并且尽我所能,两个流都在方法内关闭。

protected String storedSite; //an instance variable
/**
* Store the instance variable as text in a file
*/
public void storeAsText()
{
    PrintStream fileOut = null;
    try{
        File file = new File("StoredWebPage.txt");
        if (!file.exists()) {
            file.createNewFile();
        }

        fileOut = new PrintStream("StoredWebPage.txt");
        fileOut.print(storedSite);
        fileOut.flush();
        fileOut.close();

    } catch(Exception e) {
        if(e instanceof FileNotFoundException) {
            System.out.println("File not found");
        }
        fileOut.close();
    } finally {
        if(fileOut != null)
            fileOut.close();
    }
}

/**
* Loads the file into the program
*/
public int loadAsText()
{
    storedSite = ""; //cleansing storedSite before new webpage is stored
    Scanner fileLoader = null;
    try {
        fileLoader = new Scanner(new File("StoredWebPage.txt"));
        String inputLine;
        while((inputLine = fileLoader.nextLine()) != null)
            storedSite = storedSite+inputLine;
        fileLoader.close();
    } catch(Exception e) {
        if(e instanceof FileNotFoundException) {
            System.out.println("File not found");
            return 1;
        }
        System.out.println("an Exception was caught");
        fileLoader.close();
    } finally {
        if(fileLoader!=null)
            fileLoader.close();
    }

    return 0; //return value is for testing purposes only
}

我没有想法。为什么我不能删除我的文件?

编辑:我已经编辑了代码,但这仍然给我同样的问题:S

【问题讨论】:

  • 也许不完全是你的问题,但你确定你的代码中除了 FileNotFoundException 没有其他异常吗?例如,关闭流时出现问题等。因为捕获所有异常并仅处理 FileNotFoundException 会抛出所有其他异常,而不会让您知道发生了任何问题。
  • 另外请记住,Windows 不会让您删除任何进程打开的任何文件。

标签: java file-io


【解决方案1】:

这里有两个问题。第一个是如果在写入文件的过程中抛出异常,输出流不会关闭(读取也一样):

try {
    OutputStream someOutput = /* a new stream */;

    /* write */

    someOutput.close();

第二个问题是,如果出现异常,您不会收到通知:

} catch (Exception e) {
    if (e instanceof FileNotFoundException) {
        /* do something */
    }

    /* else eat it */
}

所以问题几乎可以肯定是抛出了一些其他异常而你不知道。

关闭流的“正确”习惯用法如下:

OutputStream someOutput = null;
try {
    someOutput = /* a new stream */;

    /* write */

} catch (Exception e) {
    /* and do something with ALL exceptions */

} finally {
    if (someOutput != null) someOutput.close();
}

或者在 Java 7 中,您可以使用 try-with-resources

【讨论】:

  • 并始终打印和/或记录堆栈跟踪。
  • 我编辑了代码,但这仍然没有解决问题编辑:我忘记为 storeastext 上的所有其他异常打印一些东西,这就是问题所在。它引发了另一个异常
  • 有什么异常?像@chrylis 建议的那样,使用e.printStackTrace(); 打印堆栈跟踪。另请记住,您不必在 catch 块中关闭。这就是 finally 块的用途。
猜你喜欢
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2010-11-02
相关资源
最近更新 更多