Java Learning--the operation of file files

Source: Internet
Author: User

* Java.io.File Class
* 1. All classes and interfaces related to input and output are defined under the Java.io package
* 2.File is a class that can have constructors to create its objects. This object corresponds to a file (. txt. avi. doc. ppt. mp3. jpg) or a file directory
* The 3.File class object is platform Independent.
* Methods in 4.File, only related to how to create, delete, rename, and so on. File is powerless as long as it involves the contents of the document and must be done by the IO stream.
* Objects of the 5.File class are often the parameters of the constructor of the concrete class of the IO stream.

1  Public classTestfile {2     /*3 * CreateNewFile ()4 Delete ()5 MkDir (): Creates a file directory. Returns true only if the upper-level file directory exists6 mkdirs (): Creates a file directory. If the upper-level file directory does not exist, create7 list ()8 listfiles ()9      */Ten @Test One      Public voidTest3 ()throwsioexception{ AFile file1 =NewFile ("D:/io/helloworld.txt"); - System.out.println (File1.delete ()); -          the         if(!file1.exists ()) { -             Booleanb =file1.createnewfile (); - System.out.println (b); -         } +          -File file2 =NewFile ("D:\\io1\\io2"); +         if(!file2.exists ()) { A             Booleanb =file2.mkdirs (); at System.out.println (b); -         } -          -File File3 =NewFile ("D:\\teach"); -string[] STRs =file3.list (); -          for(inti = 0;i < strs.length;i++){ in System.out.println (Strs[i]); -         } to          + System.out.println (); -          thefile[] Files =file3.listfiles (); *          for(inti = 0;i < files.length;i++){ $ System.out.println (Files[i].getname ());Panax Notoginseng         } -     } the      +     /* A * EXISTS () the CanWrite () + CanRead () - isfile () $ isdirectory () $ lastmodified () - Length () -  the      */ - @TestWuyi      Public voidtest2 () { theFile file1 =NewFile ("D:/io/helloworld.txt"); -File file2 =NewFile ("D:\\io\\io1"); Wu          - System.out.println (File1.exists ()); About System.out.println (File1.canwrite ()); $ System.out.println (File1.canread ()); - System.out.println (File1.isfile ()); - System.out.println (File1.isdirectory ()); -System.out.println (NewDate (File1.lastmodified ())); A System.out.println (File1.length ()); +          the System.out.println (); -          $ System.out.println (File2.exists ()); the System.out.println (File2.canwrite ()); the System.out.println (File2.canread ()); the System.out.println (File2.isfile ()); the System.out.println (File2.isdirectory ()); -System.out.println (NewDate (File2.lastmodified ())); in System.out.println (File2.length ()); the      the     } About      the     /* the * Path: the * Absolute path: Full file path including drive letter + * Relative path: The path of the file in the current file directory -      *  the * GetName ()Bayi GetPath () the Getabsolutefile () the GetAbsolutePath () - getParent () - Renameto (File newName) the  the      */ the @Test the      Public voidtest1 () { -File file1 =NewFile ("D:/io/helloworld.txt"); theFile file2 =NewFile ("Hello1.txt"); the          theFile File3 =NewFile ("D:\\io\\io1");94File file4 =NewFile ("D:\\io2"); the          the System.out.println (File1.getname ()); the System.out.println (File1.getpath ());98 System.out.println (File1.getabsolutefile ()); About System.out.println (File1.getparent ()); - System.out.println (File1.getabsolutepath ());101         102 System.out.println ();103         104 System.out.println (File3.getname ()); the System.out.println (File3.getpath ());106 System.out.println (File3.getabsolutefile ());107 System.out.println (File3.getparent ());108 System.out.println (File3.getabsolutepath ());109          the         //Renameto (File newName): Renaming111         //File1.renameto (file2): File1 renamed to File2. Requirements: File1 file must exist, file2 must not exist the         Booleanb =File1.renameto (file2);113 System.out.println (b); the          the         BooleanB1 =File4.renameto (file3); the System.out.println (B1);117}

Java Learning--the operation of file files

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.