3. Create, delete, rename methods of the file class

Source: Internet
Author: User

Generally we call the method of the built-in class, all refers to call its member method, so the following methods are the file class member methods, commonly used in the following 3 kinds,

respectively is

    //Create     Public BooleanCreateNewFile () Public Booleanmkdir () Public Booleanmkdirs ()//Delete     Public BooleanDelete ()// Renaming     Public BooleanRannameto (File dest)/*It is important to note that these methods are file built-in methods that are already in the JDK, so we do not need to create them, we can call them directly .*/

Its corresponding API is

Create:

Delete

Renaming

Below, one by one shows how these methods are used

Create a single-layer folder: mkdir

 Public class Demo {     Public Static void Main (string[] args) {        file file=new file ("E:\\demo");        System.out.println ("Create Status:" +File.mkdir ());    }     /*      1. The API method shows that the method returns a Boolean value. That is, if the creation succeeds, it returns true, and the reverse is False     2. If you are creating a path where the object is already modified, the creation fails.
3. This method can only create a layer of folders, if you want to create a multi-tier will fail: such as e:\\demo\\test, because it is the path you specified E:\\demo created test when you do not find the path you specified E:\\demo */ }
Create state: True

Create a multi-tiered folder: Mkdirs

 Public class Demo {    publicstaticvoid  main (string[] args) {        file file=  New File ("E:\\demo\\test");        System.out.println ("Create Status:" +file.mkdirs ());    }     /*      1. The API method shows that the method returns a Boolean value. That is, if the creation is successful, returns True, or False     2. If you are creating a path that already has the object modified,     the creation will fail */}
Create state: True

Create file: CreateNewFile

 public  class   Demo { public  static  void  main (string[] args) throws   ioexception {file file  =new  file (        "E:\\demo\\a.txt" );    System.out.println ( "Create Status:" +file.createnewfile ());  /*   1. The View API indicates that the built-in method throws an exception IO exception, So our caller is going to handle this one exception. But for the consistency of our code, I don't catch it, I throw it out. 2. The method throws an exception IO exception, in fact, to prevent you from creating a file when the specified directory 3 is not found. Also returns a Boolean value. When you create a file, the front path is the folder (such as demo) is certain to have or fails to create if the file already exists in the directory (such as A.txt) will also create a failure  */  
Create state: True

Delete

 Public class Demo {    // Delete file public    staticvoid  Main (string[ ] args)  {        file file=new file ("E:\\demo\\test\\a.txt");        System.out.println ("Create Status:" +File.delete ());    }     /* As the API method shows, the method returns a Boolean value. That is, if the deletion succeeds, it returns true, whereas the reverse is false* / }
 Public class Demo {    // Delete folder public    staticvoid  main (string[] args)  {        file file=new file ("E:\\demo\\test");        System.out.println ("Create Status:" +File.delete ());    }     /*      * 1. The API method shows that the method returns a Boolean value. That is, if the deletion succeeds, it returns true, whereas the reverse is false     * 2. When deleting a folder, make sure that the folder     is empty, or the deletion fails */   }

Rename: Renameto

 public  class   Demo { //  public  static        void   main (string[] args) {File file         =new  File ("E:\\demo\\test\\a.txt"  =new  file ("E:\\demo\\test\\b.txt" );    System.out.println ( "Create Status:" +file.renameto (File_change)); }}
 Public class Demo {    // rename a.txt to B.txt and cut to E:\\demo directory public    static  void  Main (string[] args)  {        file file       =new file ("E:\\demo\\test\\a.txt" );        File File_change=new file ("E:\\demo\\b.txt");                System.out.println ("Create Status:" +File.renameto (File_change));}    }

Judge

 Public classDemo {//Rename the a.txt to B.txt and cut to the E:\\demo directory     Public Static voidMain (string[] args) {File file=NewFile ("E:\\demo\\a.txt"); System.out.println ("Whether directory/folder:" +file.isdirectory ()); System.out.println ("Whether file:" +file.isfile ()); System.out.println ("Is there:" +file.exists ()); System.out.println ("Readable:" +File.canread ()); System.out.println ("Can write:" +file.canwrite ()); System.out.println ("Hide:" +File.ishidden ()); }}

Whether directory/folder: false
Whether the file: true
Is present: True
Is readable: true
is writable: true
Whether to hide: false

3. Create, delete, rename methods of 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.