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