简体   繁体   English

如何从 java 的文件夹中读取多个 csv 文件?

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

i want to read multiple csv files from a folder in java.我想从 java 的文件夹中读取多个 csv 文件。 All files are csv and with same format, but problem is the path to the folder i get is like this only.所有文件都是 csv 并具有相同的格式,但问题是我得到的文件夹的路径只是这样。

> conf/files

This is a folder where all my csv files are.这是我所有 csv 文件所在的文件夹。 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...请帮忙...

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这样,只需遍历文件名列表,并访问您已经知道的

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.如果您使用FileFilterFileNameFilter ,您可以获得文件列表以供以后迭代,或者您可以对 accept 方法中的每个文件执行某些操作。

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

使用 lambda 表达式可以在一行中实现解决方案:

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

You can easily achieve this using Files class from nio package您可以使用来自 nio package 的文件 class 轻松实现此目的

 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 filter() 将返回 Stream 的路径,你需要 map 它们使用 map() function 映射后收集到所需类型的数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM