2

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

6
  • This should be useful to you. Commented Jan 30, 2015 at 12:38
  • You can check this: stackoverflow.com/questions/1844688/read-all-files-in-a-folder Commented Jan 30, 2015 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... Commented Jan 30, 2015 at 12:43
  • @Swapnil1988 : So let us know your findings . What are you getting as result after using the above piece of code ? Commented Jan 30, 2015 at 12:46
  • list of files is null ...when i do like above Commented Jan 30, 2015 at 12:48

5 Answers 5

7
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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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"));

Comments

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

Comments

-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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.