Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read multiple csv files from a folder in java?

i want to read multiple csv files from a folder in java. All files are csv and with same format, but problem is the path to the folder i get is like this only.

> conf/files

This is a folder where all my csv files are. I have a program which read single file when path is given like

> conf/files/sample.csv

If i could get a list of file name's like these i can read all those. Please help...

like image 388
Swapnil1988 Avatar asked May 08 '26 22:05

Swapnil1988


1 Answers

List<String> filenames = new LinkedList<String>();
public void listFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else {
            if(fileEntry.getName().contains(".csv"))
                filenames.add(fileEntry.getName());
        }
    }
}

final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);

This way, only have to loop over the filenames list, and access how you already know

like image 128
Luis González Avatar answered May 11 '26 14:05

Luis González