Gets a collection of desired specified files, gets the folder (all. Java file objects that contain subdirectories, and stores them in the collection)

Source: Internet
Author: User

import java.io.file;import Java.io.filefilter;import Java.io.objectinputstream.getfield;import Java.util.arraylist;import Java.util.list;public class Huoquwenjian {/*  Gets a collection of desired specified files, gets the folder (all the. Java file objects that contain subdirectories, and stores them in the collection) * Idea: * 1, since the inclusion of subdirectories, you need recursion. * 2, in the recursive process need filter * 3, meet the conditions, are added to the collection *//** * @param args */public static void main (string[] args) {//TODO auto-generated Me               Thod stub file dir = New file ("E:\\java file");               list<file> list = new arraylist<file> ();               FileFilter filterbyjava=new Filterbyjava (". Java");               Getfilelist (Dir,list,filterbyjava);               for (File list1:list) {System.out.println (list1); }}private static void Getfilelist (File dir, list<file> list,filefilter Filterbyjava) {file[] files = dir.listfiles ( ); for (file file:files) {if (File.isdirectory ()) {getfilelist (file, list, Filterbyjava);} else{if (filterbyjava.accept (file)) {list.add (file);}}}} 
Filter class
Import Java.io.file;import Java.io.filefilter;public class Filterbyjava implements FileFilter {public String suffix; Public Filterbyjava (String suffix) {super (); this.suffix = suffix;} @Overridepublic Boolean Accept (File pathname) {//TODO auto-generated method Stubreturn pathname.getname (). EndsWith ( suffix);}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Gets a collection of desired specified files, gets the folder (all. Java file objects that contain subdirectories, and stores them in the collection)

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.