Reputation: 331
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
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
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