Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "walk around" in the File System with Java [closed]

I'd like to do a search for folders/directories from java, and go into those folders/directories in java. I guess it's called system utilities? Any tutorials out there, or books on the subject?

Thanks ;)

like image 775
Johannes Avatar asked Dec 01 '22 12:12

Johannes


2 Answers

I use this code to get all ZIP files in a folder. Call this recursively checking for the file object to be a sub directory again and again.

public List<String> getFiles(String folder) {

List<String> list = new ArrayList<String>();
        File dir = new File(folder);
        if(dir.isDirectory()) {
            FileFilter filter = new FileFilter() {

                public boolean accept(File file) {
                    boolean flag = false;
                    if(file.isFile() && !file.isDirectory()) {
                        String filename = file.getName();
                        if(!filename.endsWith(".zip")) {
                            return true;
                        }
                        return false;   
                }

            };
            File[] fileNames = dir.listFiles(filter);
            for (File file : fileNames) {
                list.add(file.getName());
            }
            return list;

}

like image 144
sangupta Avatar answered Dec 03 '22 03:12

sangupta


You could use Apache Commons FileUtils (see: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html) and specifically the listFiles method there, which can do this recursively and use filters (so it saves you the writing of the recursion yourself and answers the search you mentioned).

like image 37
Gadi Avatar answered Dec 03 '22 01:12

Gadi