Creation, renaming, and traversal of the Java IO File

Source: Internet
Author: User

A file or directory on a storage device, which can be used to view the API in a simple way

1  PackageOrg.zln.io.file;2 3 ImportJava.io.File;4 5 /**6 * Created by Coolkid on 2015/6/21 0021.7  */8  Public classTestfile {9     /*Current Program Root path*/Ten      Public Static FinalString RootPath = System.getproperty ("User.dir"); One      Public Static voidMain (string[] args) { A         //createFile1 (); - //rename (); -Showfilelist (NewFile (RootPath)); the     } -  -     /** - * Traverse Folder +      * @paramFile root directory -      */ +     Private Static voidshowfilelist (file file) { A         if(File.isdirectory ()) { atSystem.out.println ("Directory:" +File.getabsolutepath ()); -file[] Files =file.listfiles (); -              for(File file1:files) { - showfilelist (file1); -             } -}Else { inSystem.out.println ("File:" +File.getabsolutepath ()); -         } to     } +  -     /** the * Renaming of files *      */ $     Private Static voidrename () {Panax NotoginsengString mypath=rootpath+ file.separatorchar+ "MyFile"; -File File =NewFile (myPath); the         if(!file.exists ()) { + createFile1 (); A}Else { theFile NewFile =NewFile (rootpath+ file.separatorchar+ "Myfilenew"); + File.renameto (newFile); -System.out.println (Newfile.getabsolutepath () + "Create successful"); $         } $  -     } -  the     /** - * Creation of filesWuyi      */ the     Private Static voidCreateFile1 () { -String mypath=rootpath+ file.separatorchar+ "MyFile"; WuFile File =NewFile (myPath); -         if(!file.exists ()) { AboutSystem.out.println (File.getabsolutepath () + "does not exist, create"); $             if(File.mkdirs ()) { -SYSTEM.OUT.PRINTLN ("created successfully"); -}Else { -System.out.println ("Create failed"); A             } +         } the     } -}

Creation, renaming, and traversal of the Java IO File

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.