Java Learning (1): Search for all files of the desired type under one folder

Source: Internet
Author: User

Requirement: Under a folder, search for all files of the type you want (e.g. I want all txt files in a folder).

ImportJava.io.File;/*** Get all files under the desired type of file *@authorXCX * @time June 24, 2017 PM 3:02:00*/ Public classGetFile { Public Static voidMain (string[] args) {String file= "D:\\java";//Name of the folderFile f =Newfile (file);//To create a file objectString extent = ". txt";//Want to end the. txt file hereGetFiles (f,extent); }         Public Static voidGetFiles (File file,string extent) {//determine if the file is a directory        if(File.isdirectory ()) {//file is a directoryfile[] files = file.listfiles ();//get all the files under the file//traverse these files to see if any files are being asked             for(File f:files) {//traverse these files for recursionGetFiles (f, extent); }            }Else{//file is not a directory//get the file path and decide if you want to end itString Path =File.getpath (); if(Path.endswith (extent)) {//is the required file, the output pathSystem.out.println (path); }        }    }}

This code can be used when searching for files.

Java Learning (1): Search for all files of the desired type under one folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.