znaya
znaya

Reputation: 331

reading filenames from a folder in a project in Eclipse

i'm using Eclipse 4.2.0 and I need to list all the filenames from a folder imported in a project. that folder is "account_info" and is right under the project home folder.

the method i'm using is

List<String> account_files = new ArrayList<String>();
File[] files = new File("account_info").listFiles();

            for (File file : files) {
                if (file.isFile()) {
                    account_files.add(file.getName());
                }
            }
            for(int i = 0; i < account_files.size(); i++){
                System.out.println(account_files.get(i));
            }

but... no luck! i also tried referring to that folder using the path "../account_info".

thanks, in advance, for any help.

Upvotes: 1

Views: 2804

Answers (2)

Teo
Teo

Reputation: 3213

Try this:

final File folder = new File("/home/yoursName/....../Desktop/account_info");

allFiles(folder);

where allFiles is:

    public List<String> allFiles(final File folder) {
         List<String> account_files = new ArrayList<String>();

        for (File file : folder.listFiles()) {
                account_files.add(file.getName());
        }
return account_files;
    }

Upvotes: 0

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13736

Try something like this:

It is pointing to the correct path in your code. System.getProperty("user.dir") tells you the source you need point to fetch the records.

import java.io.File;
import java.util.ArrayList;
import java.util.List;

    public class Test{
          public static void main(String[] args) {
        List<String> account_files = new ArrayList<String>();
        System.out.println(System.getProperty("user.dir"));
        File[] files = new File(System.getProperty("user.dir")+"/account_info").listFiles();
        System.out.println(files.length);

                    for (File file : files) {
                        if (file.isFile()) {
                            account_files.add(file.getName());
                        }
                    }
                    for(int i = 0; i < account_files.size(); i++){
                        System.out.println(account_files.get(i));
                    }
          }
    }

Upvotes: 2

Related Questions