从文件夹及其子文件夹中查找特定的文件类型 [英] find specific file type from folder and its sub folder

查看:397
本文介绍了从文件夹及其子文件夹中查找特定的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种从文件夹和子文件夹中获取特定文件类型(如pdf或txt)的方法,但我无法解决此问题。这是我的代码

I am writing a method to get specific file type such as pdf or txt from folders and subfolders but I am lacking to solve this problem. here is my code

  // .............list file
    File directory = new File(directoryName);

    // get all the files from a directory
    File[] fList = directory.listFiles();

    for (File file : fList) {
        if (file.isFile()) {
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath());
        }
    }

我当前的方法列出了所有文件,但我需要特定的文件

My current method list all files but I need specific files

推荐答案

if(file.getName().endsWith(".pdf")) {
    //it is a .pdf file!
}

/ * ** /

/***/

这篇关于从文件夹及其子文件夹中查找特定的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆