0

As an exercise for school I wrote a method in Java that searches for a character in a file. Here is the code:

 public static void countLetter(char needle, String hayStack) throws IOException {

        File f = new File(hayStack);                                           
        try (Scanner in = new Scanner(f)) {                                    
            String str = null;                                                 
            while (in.hasNext()){                                              
                str += in.next();                                              
            }                                                                  
            char[] charArr = str.toCharArray();                                
            int counter = 0;                                                   
            for (char c: charArr) {                                            
                if (c == needle){                                              
                    counter++;                                                 
                }                                                              
            }                                                                  
            System.out.println(counter);                                       
        }                                                                      
    }

This does what I need it to but I have a question. Is the file object ever opened? And if it is, does it ever closed? I used try-with-resources on the Scanner object so I'm pretty sure I don't have to explicitly close that, but what about the file object?

4

3 回答 3

3

文件对象只是路径名的抽象表示,它与打开文件无关。所以不能关闭。

于 2013-05-06T01:31:12.240 回答
1

File对象无法打开(因此无法关闭),因为它是文件路径的表示,而不是文件的表示。

Scanner课程打开您的文件以阅读它。您的程序不会调用in.close(),因此当您的方法返回Scanner时不会被垃圾收集,因为它仍然具有对打开文件的引用。您也可能会锁定文件,具体取决于Scanner.

为了使类有资格自动关闭 try-resource 块中的资源,该类必须实现java.lang.AutoCloseable(或java.io.Closeable)。Scanner没有实现这些接口,所以当你退出 try-resource 块时它不会close()调用它的方法。

将代码更改为:

public static void countLetter(char needle, String hayStack) throws IOException {

    File f = new File(hayStack);                                           
    try (Scanner in = new Scanner(f)) {                                    
        String str = null;                                                 
        while (in.hasNext()){                                              
            str += in.next();                                              
        }                                                                  
        char[] charArr = str.toCharArray();                                
        int counter = 0;                                                   
        for (char c: charArr) {                                            
            if (c == needle){                                              
                counter++;                                                 
            }                                                              
        }                                                                  
        System.out.println(counter);
        in.close();
    }
}
于 2013-05-06T02:05:04.800 回答
0

由于该变量是您的函数的本地变量,因此一旦函数存在,它将超出范围,并最终进行垃圾收集。您的问题的答案是否定的,在您的情况下,文件对象从未打开过。

于 2013-05-06T01:26:32.193 回答