Java file Operations (3)

Source: Internet
Author: User

The display of all sub-files and folders under the folder:

Package xz_file;import Java.io.file;import java.io.IOException; Public classExfile { Public Static voidMain (string[] args) {File file=NewFile ("c:/users/xz/workspaces/myeclipse 9/xzlearn");            ListFile (file); }         Public Static voidlistfile (file file) {file lf[]=File.listfiles ();  for(inti =0; i < lf.length; i++) {            if(Lf[i]! =NULL) {                Try {                    if(Lf[i].isfile ()) {System. out. println (Lf[i].getname ()); } Else if(Lf[i].isdirectory ()) {System. out. println (Lf[i].getname ());                    ListFile (Lf[i]); }                } Catch(Exception e) {//Todo:handle ExceptionSystem. out. println ("File type Error! "); }            }        }    }}

Results:. classpath
. Project
. settings
Org.eclipse.jdt.core.prefs
Bin
Xz_file
Exfile.class
Src
Xz_file
Exfile.java
Xz
Eq.txt
Xz1
13.txt
Xz2
123e.txt
Weqwe.txt
Xz3
Xz4
Qdawe.txt

Delete all files under folder:

Package xz_file;import Java.io.file;import java.io.IOException; Public classExfile { Public Static voidMain (string[] args) {File file=NewFile ("c:/users/xz/workspaces/myeclipse 9/xzlearn/xz");            DeleteFile (file); }         Public Static voiddeletefile (file file) {file lf[]=File.listfiles ();  for(inti =0; i < lf.length; i++) {            if(Lf[i].isdirectory () &&lf[i]!=NULL) {DeleteFile (lf[i]); }Else if(Lf[i].isfile ()) {lf[i].delete (); }Else{lf[i].delete (); }                        }    }}

Results: C:\Users\xz\Workspaces\MyEclipse 9\xzlearn\xz\xz1\xz2\xz3\xz4

The file is deleted, but the folder is preserved, so you need to modify the code. Give the dog a little time, take it slow.

Java file Operations (3)

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.