user7600158
user7600158

Reputation:

How get all files in folder in Java

I have project and I don't know how to get all file names in folder. I work in Andoid Studio. I just need to make function getAllFilesInFolder(string folderPath). I'd be happy if you write all the imports that it needs too. Code I use:

package com.my.appPackage;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] files = getAllFilesInFolder("/sdcard/folderIUse");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.LargeTextInList, files);
        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new ItemList());
    }
    class ItemList implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            ViewGroup vg = (ViewGroup) view;
            TextView tv = (TextView) vg.findViewById(R.id.LargeTextInList);
            Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    }

}

Upvotes: 1

Views: 8949

Answers (4)

Siniša
Siniša

Reputation: 154

This might work:

    ArrayList<String> result = new ArrayList<String>(); //ArrayList cause you don't know how many files there is
    File folder = new File("PATH/TO/YOUR/FOLDER/AS/STRING"); //This is just to cast to a File type since you pass it as a String
    File[] filesInFolder = folder.listFiles(); // This returns all the folders and files in your path
    for (File file : filesInFolder) { //For each of the entries do:
        if (!file.isDirectory()) { //check that it's not a dir
            result.add(new String(file.getName())); //push the filename as a string
        }
    }

    return result;

Upvotes: 3

Ayush Bansal
Ayush Bansal

Reputation: 722

Let's say you want to gather all the files in storage/emulated/0/DCIM and "DCIM" futher has two directories let's say Screenshots and pictures.This is how you would do it .

public void  getAllFiles(File parent) {
    List<File> directories = new ArrayList<>();
    List<File> files = new ArrayList<>();
    //adding initial parent whose files we want to fetch
    directories.add(parent);
    while (directories.size() != 0) {
        File f = directories.remove(0);
        if (f.isDirectory()) {
            if (f.list().length > 0) {
                //directory filter to filter out directories if any
                List<File> directoryList = Arrays.asList(f.listFiles(directoryFilter));
                //file filter to filter out files if any
                List<File> fileList = Arrays.asList(f.listFiles(fileFilter));
                //adding directories to directory list
                directories.addAll(directoryList);
                //adding files to file list
                files.addAll(fileList);
            }
        }
    }
}

FilenameFilter fileFilter = new FilenameFilter() {
    @Override
    public boolean accept(File file, String s) {
    return file.isFile();
    }
    };

    FilenameFilter directoryFilter = new FilenameFilter() {
        @Override
        public boolean accept(File file, String s) {
            return file.isDirectory();
        }
    };

This way one has to just pass the folder name to the function to get all the files in files object.Hope this helps

Upvotes: 2

BananZ
BananZ

Reputation: 1193

The answer from Mehran Zamani will work but remember to check the file type. Remember don't just check for the extension only like "abc.txt". The abc file maybe is a PDF file but somebody changed its extension by simply rename it. You have to check its mime type for safety purpose. For example in Java 7 you can now just use Files.probeContentType(path). This is just an example, you might need to search for a better solution online. Just trying to give you the idea

You might need to check whether if it is another directory. for example file.isFile() / file.isFolder().

Upvotes: 0

Mehran Zamani
Mehran Zamani

Reputation: 831

File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
    //do something
}

path should be something like this: /storage/emulated/0/Android/data

Upvotes: 1

Related Questions