Java file class and recursive tree display directory _20150804

Source: Internet
Author: User

Java file class and recursive tree display directory

20150804


The following constructor can be used to generate a file object:

File (String directorypath) File (String directorypath, string filename)file (file dirobj, String filename)here, DirectoryPath is the path name of the file, filename is the name, and DirObj is a specified directory file object. The following example creates three files: F1,f2, and F3. The first file object is generated by a constructor that has only one directory path parameter. The second object has two parameters--path and file name. The parameters of the third file object include the path and file name that points to the F1. F3 and F2 point to the same file. File F1 = new file ("/");file F2 = new file ("/", "Autoexec.bat");file F3 = New file (F1, "Autoexec.bat");Note: Java can correctly handle UNIX and Windows/dos contract path separators. If you use a slash (/) in the Windows version of Java, the path processing is still correct. Remember, if you use Windows/dos with a backslash (/) Convention, you need to use its escape sequence (//) within the string. Java conventions are path separators using UNIX and URL-style slashes.


Here is a small example of how the file class is commonly used:

Objective: To print the specified directories and files on the console in a tree-shaped structure

Thought: Recursion

Package day04;import java.io.file;/* * Print Tree Directory * Core method: Recursive * method used: *   1) f.getname ()---Get the name of the directory *   2) f.list ()------listed All the first level directories and files in this directory, return value string[] *     */public class TestFile02 {public static void Printfile (file f,int lever) {for (int i=0;i <lever;i++) {System.out.print ("-");} System.out.println (F.getname ()); if (F.isfile ()) {return;} Else{string[] S=f.list (); for (int i=0;i<s.length;i++) {file F1=new file (F.getpath () +file.separator+s[i]); Printfile (f1,lever+1);}} return;} public static void Main (string[] args) {//string path= "/HOME/SOFT01/QXL";//Can be used/ You can also use the delimiter File.separator provided by the file class (the suggested delimiter, there is no system problem) String path= "f:/javatrain/teaching"; File F=new file (path);p rintfile (f,0);}}


Summary: The file class commonly used methods and properties are relatively simple, basically see the name know, no longer verbose. Special several in the above code have a demo




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

Java file class and recursive tree display directory _20150804

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.