我刚刚开始使用apache commons vfs 2.0。我试图在c:\
中获取文件夹的文件列表,但是c:\document and setting
出错了。这是代码:
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());
}
}
}
错误:
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)
答案 0 :(得分:0)
您必须使用适当的协议通过VFS连接试试这个:
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());
}
}
}