Java: how does isDirectory and isFile work in File-class?

Posted by HH on Stack Overflow See other posts from Stack Overflow or by HH
Published on 2010-04-10T13:29:09Z Indexed on 2010/04/10 13:33 UTC
Read the original article Hit count: 380

Filed under:
|
|
$ javac GetAllDirs.java 
GetAllDirs.java:16: cannot find symbol
symbol  : variable checkFile
location: class GetAllDirs
        System.out.println(checkFile.getName());
                           ^
1 error
$ cat GetAllDirs.java 
import java.util.*;
import java.io.*;
public class GetAllDirs {
    public void getAllDirs(File file) {
        if(file.isDirectory()){
            System.out.println(file.getName());
            File checkFile = new File(file.getCanonicalPath());
        }else if(file.isFile()){
            System.out.println(file.getName());
            File checkFile = new File(file.getParent());
        }else{
                    // checkFile should get Initialized at least HERE!
            File checkFile = file;
        }
        System.out.println(file.getName());
        // WHY ERROR HERE: checkfile not found
        System.out.println(checkFile.getName());
    }
    public static void main(String[] args) {
        GetAllDirs dirs = new GetAllDirs();     
        File current = new File(".");
        dirs.getAllDirs(current);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about file