Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a filesystem in java?

I need to iterate a folder on local machine from the server to parse some files. Is it real? If it's true, please, advise me how to do it.

like image 462
DmitryB Avatar asked Mar 26 '12 08:03

DmitryB


2 Answers

I prefer Commons VFS. It can handle local filesystems, SFTP and many others. All with the same code - you just change paths to files.

FileSystemManager fsManager = VFS.getManager();
FileObject directory = fsManager.resolveFile("path/to/dir");
FileObject[] files = directory.findFiles(fileSelector);

for (FileObject file : files) {
    // do something
}
like image 77
user219882 Avatar answered Oct 13 '22 20:10

user219882


You can also have a glance at the new package nio.2 in Java 7, with tutorial here.

Many new and powerful things.

like image 28
cl-r Avatar answered Oct 13 '22 20:10

cl-r