1

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...

Swapnil1988
  • 269
  • 3
  • 6
  • 18
  • [This](http://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter) should be useful to you. – SME_Dev Jan 30 '15 at 12:38
  • You can check this: http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder – Luis González Jan 30 '15 at 12:39
  • Hi i have tried like this csvFIlePath is "conf/files" File dir = new File(csvFilePath); File[] listOfFiles = dir.listFiles(); But i got listOfFiles as null... – Swapnil1988 Jan 30 '15 at 12:43
  • @Swapnil1988 : So let us know your findings . What are you getting as result after using the above piece of code ? – The Dark Knight Jan 30 '15 at 12:46
  • list of files is null ...when i do like above – Swapnil1988 Jan 30 '15 at 12:48
  • Thanks guys for answering but i have solved my problem by dir = new File(FmceUtils.class.getClassLoader().getResource(csvFilePath).toURI()); File[] listOfFiles = dir.listFiles(); "conf/files"+file.getName(); Gives me what i wanted to read csv's thanks again – Swapnil1988 Jan 30 '15 at 15:45

5 Answers5

6
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

Luis González
  • 3,199
  • 26
  • 43
2

If you use FileFilter or FileNameFilter, you can get either a list of files for a later iteration or you can do something with every single file inside in the accept method.

String path = "conf/files";

File [] files = new File(path).listFiles(new FileFilter() {
    @Override
    public boolean accept(File path) {
        if(path.isFile()) {
            //Do something with a single file
            //or just return true to put it in the list
            return true;
        }
        return false;
    }
}); 
//Do something with the list of files
Gren
  • 1,850
  • 1
  • 11
  • 16
2

With a lambda expression a solution can be achieved in one line:

File [] files = new File("path/to/folder/").listFiles(obj -> obj.isFile() && obj.getName().endsWith(".csv"));
Stefanos
  • 909
  • 12
  • 19
0

You can easily achieve this using Files class from nio package

 RequiredType type = 
        Files.walk(Paths.get("conf/files"), 2)
                .filter(x -> x.getFileName().toString().endsWith(".csv"))
                .map(x -> {mapping logic})
                .toArray(RequiredType[]::new);

filter() will return Stream of Path, you'll need the map them using map() function after mapping them collect in the array of required type

-1
Another way to read all .csv file in your folder directory by directory.

String inputFolder = "E:\\CSVFiles\\take";//input path where you read the file 
File folder = new File(inputFolder); 
List<String> listOfFiles=listDirectory(folder, 0);  //0 is the level where we start reading

//This method for Reading the file in directory manner(folder under folder all the csv)
private static List<String> listDirectory(File file, int level) {

    List<String> lstFiles = new ArrayList<String>();

    File[] firstLevelFiles = file.listFiles();
    if (firstLevelFiles != null && firstLevelFiles.length > 0) {
     for (File aFile : firstLevelFiles) {
      if (aFile.isDirectory() && !"ProcressedEMLs".equalsIgnoreCase(aFile.getName())
        && !"FailedEMLs".equalsIgnoreCase(aFile.getName())) {
       lstFiles.addAll(listDirectory(aFile, level + 1));
      } else if (!aFile.isDirectory()) {
       if (aFile.toString().toLowerCase().endsWith(".csv")) {
        lstFiles.add(aFile.getAbsolutePath());
       }
      }
     }
    }
    firstLevelFiles = null;
    return lstFiles;
}
Ashish
  • 110
  • 4
  • 14