search
HomeJavajavaTutorialHow to traverse all PDF files under a certain path in java

How to traverse all PDF files under a certain path in java

Nov 30, 2019 pm 01:16 PM
javapdf filepathTraverse

How to traverse all PDF files under a certain path in java

目标:

找出某路径下的所有PDF文件,当然,也适用于找出所有文件、所有txt文件等等。

输入:

任意一个路径(可以是文件或文件夹)。

输出:

该路径下所有PDF文件的路径。

思路:

根据路径字符串是否以“.pdf”结尾来判断是否是PDF文件。

首先判断输入路径是文件还是文件夹,是文件则直接判断是否是PDF文件,是文件夹,则获取其下所有的文件(夹)对象集合,存到LinkedList对象中,然后遍历LinkedList,每次获取并移除第一个对象进行判断,对象是文件则直接判断是否是PDF文件,对象是文件夹则获取并遍历其下所有文件对象。

相关免费视频教程推荐:java免费视频教程

注意:

是文件则直接判断是否是PDF文件。是文件夹,则将该对象加入到LinkedList中。

代码示例如下:

package load_select;
 
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
 
public class FindAllFiles {
 
	public static void folderMethod1(String path) {
		
        File file = new File(path);
        LinkedList<File> list = new LinkedList<>();
        
        //保存所有pdf文件的对象
        LinkedList<File> pdfList = new LinkedList<File>();
 
        //该路径对应的文件或文件夹是否存在
        if (file.exists()) {
        	
        	//如果该路径为---文件或空文件夹
            if (null == file.listFiles()) {
//            	System.out.println(file.getAbsolutePath());
            	if(file.getAbsolutePath().endsWith(".pdf")) 
            		pdfList.add(file);
            }
            
            //如果该路径为非空文件夹
            else {
            	//将该路径下的所有文件(文件或文件夹)对象加入队列
                list.addAll(Arrays.asList(file.listFiles()));
                //遍历该队列     
                while (!list.isEmpty()) {
              	
                	File firstF = list.removeFirst();
                	
                	//这里不论是文件夹还是文件,只需判断是否以“.pdf”结尾
            		if(firstF.getAbsolutePath().endsWith(".pdf")) 
            			pdfList.add(firstF);
                	
                	File[] files = firstF.listFiles();
                  
                	if (null == files) {
                		//System.out.println(firstF.getAbsolutePath());
                		continue;
                	}
                	for (File f : files) {
                		if (f.isDirectory()) {
                			//System.out.println("文件夹:" + f.getAbsolutePath());
                			list.add(f);
                		} else {
                			//System.out.println("文件:" + f.getAbsolutePath());
                			
                			if(f.getAbsolutePath().endsWith(".pdf")) 
                    			pdfList.add(f);
                			
                		}
                	}
              }
            }
 
        } else {
            System.out.println("文件不存在!");
        }
        
        //输出所有pdf文件的路径
        for(File f : pdfList) 
        	System.out.println(f.getAbsolutePath());
        
    }
	
	public static void main(String[] args) {
		String path = "D:\\File\\pdf表格识别";    //改为自己的路径
		folderMethod1(path1);
	}
}

部分结果如图:

How to traverse all PDF files under a certain path in java

相关文章教程推荐:java入门

The above is the detailed content of How to traverse all PDF files under a certain path in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor