Java article: Basic API for the file class

Source: Internet
Author: User

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

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.