有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

文件delete()不会删除文件,java

好吧,这会有点长。所以我制作了一个junit测试类来测试我的程序。我想测试一个使用扫描器将文件读入程序的方法是否引发异常,如果文件不是这样存在的:

@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


共 (1) 个答案

  1. # 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();
    }
    

    或者在Java7中,可以使用try-with-resources