[JavaSE] IO stream (recursive FIle list of FIle objects), javaseio

Source: Internet
Author: User

[JavaSE] IO stream (recursive FIle list of FIle objects), javaseio

Get the File object, new, and construct the parameter: String directory name

Call the list () method of the File object to obtain the name of the String [] array File.

Loop array, listing all files including hidden files

 

Recursively list all data

Define a static method showDir () and pass it into the parameter: FIle object

Call the listFiles () method of the File object to obtain the array of File [].

Loop File [] array, each element is a File object

In the loop, call the isDirectory () method of the File object to determine whether it is a directory. If it is a directory, call the showDir () method again. Parameter: File object

If the File object is not printed

Import java. io. file; public class FileDemo {/*** @ param args */public static void main (String [] args) {File file = new File ("E: /adt-bundle-windows/workspace/IpTest "); showDir (file );} /*** recursively display the file ** @ param File */public static void showDir (file File) {System. out. println ("Directory:" + file); File [] files = file. listFiles (); for (File dir: files) {// conditions if (dir. isDirectory () {showDir (dir);} else {System. out. println ("file:" + dir );}}}}

PHP version:

<? Phpclass FileDemo {public static function main () {FileDemo: showDir ("E:/adt-bundle-windows/workspace/IpTest ");} /*** recursively display the file * @ param file */public static function showDir ($ file) {echo "Directory :". $ file. "<br/>"; $ dirObj = dir ($ file); while ($ files = $ dirObj-> read ()) {// conditional if (is_dir ($ file. "/". $ files) & $ files! = "." & $ Files! = ".. ") {FileDemo: showDir ($ file. "/". $ files) ;}else {echo "& nbsp; file :". $ files. "<br/>" ;}}} FileDemo: main ();

 

Related Article

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.