Search code examples
apachevfs

org.apache.commons.vfs2.FileNotFolderException error navigating through folder using commons vfs 2.0


I just started to use apache commons vfs 2.0. I was trying to get file list of folder in c:\ but got error with c:\document and setting. Here is the code:

FileSystemManager fsm = VFS.getManager();
    FileObject fo=fsm.resolveFile("c:\\");
    FileObject[] fos=fo.getChildren();
    for(FileObject f:fos){
        FileType ft=f.getType();
        if(f.getType().equals(FileType.FOLDER)){
                for( FileObject fc:f.getChildren()){
                    System.out.println(fc.getName().getPath());
                }
        }
    }

Error:

org.apache.commons.vfs2.FileNotFolderException: Could not list the contents of "file:///c:/Documents and Settings" because it is not a folder.
    at org.apache.commons.vfs2.provider.AbstractFileObject.getChildren(AbstractFileObject.java:693)
    at mypack.gui.FileExplorer.<init>(FileExplorer.java:31)
    at mypack.gui.FileExplorer.main(FileExplorer.java:42)

Solution

  • You have to use proper protocol to connect via VFS Try this:

    FileSystemManager fsm = VFS.getManager();
    FileObject fo=fsm.resolveFile("file:///c:/");
    FileObject[] fos=fo.getChildren();
    for(FileObject f:fos){
        FileType ft=f.getType();
        if(f.getType().equals(FileType.FOLDER)){
                for( FileObject fc:f.getChildren()){
                    System.out.println(fc.getName().getPath());
                }
        }
    }