java--get all file names under the specified folder

Source: Internet
Author: User

Recursively gets all the filenames, paths, and directories under a directory.

1  Packagetest;2 3 ImportJava.io.File;4 5  Public classgetfilename{6 7      Public Static voidMain (string[] args) {8         //This is the path where the file ' s name want to take.9String Path = "C:\\programdata";Ten getFile (path); One     } A  -     Private Static voidgetFile (String path) { -         //get file list where the path has theFile File =NewFile (path); -         //Get the Folder List -file[] Array =file.listfiles (); -  +         if(Array! =NULL)//No access, the error file is null -              for(inti = 0; i < Array.Length; i++) { +                 if(Array[i].isfile ()) {//can be judged in this way if (obj instanceof File) {   A                     //Only take file name atSystem.out.println ("^^ ^^ ^" +array[i].getname ()); -                     //Take file path and name -System.out.println ("#####" +array[i]); -                     //Take file path and name -SYSTEM.OUT.PRINTLN ("* * * *" +Array[i].getpath ()); -}Else if(Array[i]instanceofFile) { in GetFile (Array[i].getpath ()); -                 } to             } +     } -}

To return these things,

The

 Packagetest;ImportJava.io.File;/*** * Get all files in the specified directory (excluding folders), using recursion * *@paramobj *@return  */   Public StaticArraylist<file>getlistfiles (Object obj) {File directory=NULL; if(objinstanceofFile) {Directory=(File) obj; } Else{directory=NewFile (obj.tostring ()); } ArrayList<File> files =NewArraylist<file>(); if(Directory.isfile ()) {files.add (directory); returnfiles; } Else if(Directory.isdirectory ()) {file[] Filearr=Directory.listfiles ();  for(inti = 0; i < filearr.length; i++) {File Fileone=Filearr[i];          Files.addall (Getlistfiles (Fileone)); }      }      returnfiles; }  

Reference: http://blog.csdn.net/tomorrowzm/article/details/3693653

http://hw1287789687.iteye.com/blog/1946488

java--get all file names under the specified 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.