java 如何从java中的文件夹中读取多个csv文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28236098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read multiple csv files from a folder in java?
提问by Swapnil1988
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.
我想从 java 的文件夹中读取多个 csv 文件。所有文件都是 csv 并且格式相同,但问题是我得到的文件夹的路径只是这样。
> 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
这是我所有 csv 文件所在的文件夹。我有一个程序,当给出路径时读取单个文件,例如
> conf/files/sample.csv
If i could get a list of file name's like these i can read all those. Please help...
如果我能得到像这样的文件名列表,我可以阅读所有这些。请帮忙...
回答by Luis González
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
这样,只需要遍历文件名列表,并访问您已经知道的
回答by Gren
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.
如果您使用FileFilter
or FileNameFilter
,您可以获得文件列表以供以后迭代,或者您可以对 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
回答by Ashish
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;
}