Java version of folder Traversal

Source: Internet
Author: User

Java version of folder Traversal

The File class in Java (the File class, in fact, feels that the File class is a little misleading, it would be better to use the "File path") has a list method, and will return all the files and directories in the specified path, this method and simple recursion can be used to write a simple file traversal program.


import java.io.File;import java.io.FileWriter;import java.io.IOException;public class test_for_team_study_2{public static void traverse(String name, FileWriter wt) throws IOException{wt.write(name + "\r\n");File path = new File(name);String[] list = path.list();if( null == list )return ;for(int i = 0; i < list.length; i ++)if( -1 == list[i].indexOf(".") )traverse(name + "/" + list[i], wt);}public static void main(String[] args) throws IOException{FileWriter wt = new FileWriter("C:/Users/Administrator/Desktop/file in D.out");String name = "D:/";File path = new File(name);String[] list = path.list();for(int i = 0; i < list.length; i ++){wt.write("now is the file#:" + i + "\r\n==========================\r\n");traverse(name + "/" + list[i], wt);wt.write("\r\n\r\n\r\n");}wt.flush();wt.close();System.out.println("finished!");}}


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.