简体   繁体   English

从包含多个文件类型的Java目录中读取单个文件类型

[英]read single file type from directory containing multiple file types java

Is it possible to read single type of file (say like CSV or txt) from the directory containing multiple file types (say like cvs , txt , doc, xml, html, etc..) 是否可以从包含多种文件类型(例如cvs,txt,doc,xml,html等)的目录中读取单一类型的文件(例如CSV或txt)。

My problem is I have to provide path of the mainDirectory as an input , which further has layers of subdirectories inside it. 我的问题是我必须提供mainDirectory的路径作为输入,它的内部进一步包含子目录层。 I specifically need to read and process CSV files within these directories as I further dive in. 在进一步深入研究时,我特别需要读取和处理这些目录中的CSV文件。

I am done with multiple layers of folder traversing using recursion courtesy which I have the names and total count of the files within the mainDirectory. 我通过递归礼貌完成了多层文件夹遍历,我拥有了mainDirectory中文件的名称和总数。 I am also done with logic to read and CSV files. 我也已经完成了读取和CSV文件的逻辑。 All I need to do is to get path only of CSV files and process them. 我需要做的就是仅获取CSV文件的路径并进行处理。

i am using below mentioned code for traversing multiple folders and getting the name :- 我正在使用下面提到的代码来遍历多个文件夹并获取名称:-

package org.ashu.input;

import java.io.File;

/**
 *
 * @author ashutosh
 */
public class MultiDir {

    private int fileCount;

    public void readFile(File f){
        System.out.println(f.getPath());
        fileCount++;
    }

    public void readDir(File f){
        File subdir[] = f.listFiles();
        for(File f_arr : subdir){
            if(f_arr.isFile()){
                this.readFile(f_arr);
            }
            if(f_arr.isDirectory()){
                this.readDir(f_arr);
            }
        }
    }

    public static void main(String[] args){
        MultiDir md = new MultiDir();
        File mainDir = new File("/Users/ashutosh/Downloads/main_directory");
        md.readDir(mainDir);
        System.out.println(md.fileCount);//all file count = 1576, need to specifically get CSV
    }

}

Any suggestion please. 请提出任何建议。

You can simply check the extension of the file in your readDir() method. 您可以在readDir()方法中简单地检查文件的扩展名。 The below looks for jpg, you can use your desired extension 下面寻找jpg,您可以使用所需的扩展名

public void readDir(File f){
    File subdir[] = f.listFiles();
    for(File f_arr : subdir){
        if(f_arr.isFile() && f_arr.getName().endsWith(".jpg")){
            this.readFile(f_arr);
        }
        if(f_arr.isDirectory()){
            this.readDir(f_arr);
        }
    }
}

This code will return every file matching the given extension (in your case .csv ): 此代码将返回与给定扩展名(在您的情况下为.csv )匹配的每个文件:

public static List<File> getFiles(String extension, final File folder)
{

    extension = extension.toUpperCase();

    final List<File> files = new ArrayList<File>();
    for (final File file : folder.listFiles())
    {

        if (file.isDirectory())
            files.addAll(getFiles(extension, file));
        else if (file.getName().toUpperCase().endsWith(extension))
            files.add(file);

    }

    return files;

}

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

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