1 PackageAres.io;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 6 Public classMain {7 8 Public Static voidMain (string[] args) {9 //TODO auto-generated Method StubTenFile File =NewFile ("C:\\javas"); One //file File1 = new file ("C:" +file.separator+ "Javas");//file.separator separators, common to multiple systems A if(!file.exists ()) { -File.mkdir ();//Create a folder - //file.mkdirs ();//Create a multilevel catalog the}Else { -File.delete ();//Delete directory - } - +System.out.println (file);//Direct Access is file.tostring () -System.out.println (File.isdirectory ());//whether it is a directory +System.out.println (File.isfile ());//whether it is a file ASystem.out.println (File.getabsolutepath ());//Get Path atSystem.out.println (File.getname ());//get File (folder) name - -File file2=NewFile ("c:\\javas\\ log. txt"); - //file File2=new file ("C:\\javas", "Log. txt");//the second way to create a file heap box - if(!file2.exists ()) { - Try { inFile2.createnewfile ();//Create a file -}Catch(IOException e) { to //TODO auto-generated Catch block + e.printstacktrace (); - } the}Else { * File2.delete (); $ }Panax NotoginsengSystem.out.println (File2.getparent ());//Get Parent Directory -System.out.println (File2.getname ());//Get file name the } + A}
1 /*2 * Traverse file directory3 */4 Public Static voidListDirectory (File dir)throwsIOException {5 if(!dir.exists ()) {6 Throw NewIllegalArgumentException ("directory" + dir + "does not exist");7 }8 if(!dir.isdirectory ()) {9 Throw NewIllegalArgumentException (dir + "not directory");Ten } One System.out.println (dir); Afile[] Files =dir.listfiles (); - //string[] filenames= dir.list ();//file (folder) an array group under directory - if(Files! =NULL&& files.length > 0) { the for(File file:files) { - System.out.println (file); - if(File.isdirectory ()) { - listdirectory (file); + } - } + } A}
Java article: Basic API for the file class