Java's file

Source: Internet
Author: User

1  Public classDemo1_file {2 3     /*4 * File (String pathname) Gets the file object based on a path5 * File (string parent, string child) Gets the file object based on a directory and a sub-file/directory6 * File (file parent, String Child) obtains the file object from a parent class file object and a subclass files/directory7      */8      Public Static voidMain (string[] args) {9         //demo1 ();Ten         //Demo2 (); One         //The parent directory is encapsulated as an object because it allows you to manipulate some methods of the file class AString parent = "D:\\test"; -String child = "Test.txt"; -File File =NewFile (parent); theFile file2 =Newfile (file, child); - System.out.println (File2.exists ()); -  -     } +  -     /** + * Separating the parent directory from the child directory allows for greater flexibility and easier modification of the corresponding directory A      */ at      Public Static voidDemo2 () { -String parent = "D:\\test"; -String child = "Test.txt"; -File File =NewFile (parent, child); - System.out.println (File.exists ()); -     } in  -      Public Static voiddemo1 () { toFile File =NewFile ("D:\\test.txt"); + System.out.println (File.exists ()); -     } the  *}
1  Public classDemo2_file {2 3     /*4 * Create features:5 * Public boolean createnewfile () create file, if file exists, no longer create return false6 * Public boolean mkdir () creates a folder and returns False if there is no longer created7 * Public boolean mkdirs () creates a multi-level directory that will be created if the parent directory does not exist8 * Note: If you do not add a letter, you will default to the current project directory9      * Ten * Rename function: One * Public boolean Renameto (file dest) renames files to the specified file path A * Public Boolean Delete () to delete a file or folder - * Renaming considerations: - * If the path name is the same, it is only renamed, if the pathname is different, it is renamed and cut the * Removal feature notes: - * Delete in Java does not go to recycle Bin; - * To delete a folder, the folder can not contain files or folders, or it will delete the failure -      *  + * Judging function: - * Public boolean isdirectory () to determine if it is a directory + * Public boolean isfile () determines whether the file is A * Public Boolean exists () to determine if a file exists at      *          - * Public boolean canRead () to determine if the file is readable - * Public boolean canWrite () to determine if writable - * Public boolean Ishidden () to determine whether to hide -      *  - * Get Features: in * Public String GetAbsolutePath () Gets the absolute path - * Public String GetPath () Gets the path passed in the constructor method to * Public String getName () Gets the file or folder name + * Public long length () gets length, number of bytes - * Public long lastmodified () Gets the last modified time, millisecond value the * Public sting[] List () gets an array of names of all files or folders under the specified directory (just get the file name) * * Public file[] Listfiles () gets file data for all files or folders in the specified directory (with file absolute path) $      *         Panax Notoginseng      */ -     /** the      * @paramargs +      * @throwsIOException A      */ the      Public Static voidMain (string[] args)throwsIOException { +          -         //demo1 (); $         //Demo2 (); $         //Demo3 (); -     } -  the     /** - * Judging functionWuyi      */ the      Public Static voidDemo3 () { -File File =NewFile ("AA"); WuFile.setreadable (false);//All files are considered readable in the Windows system -File.setwritable (false);//can be set to not writable in Windows system About     } $  -     /** - * Rename and delete functions -      */ A      Public Static voidDemo2 () { +File File =NewFile ("CCC"); theSystem.out.println (File.renameto (NewFile ("AAAA"))); -File file1 =NewFile ("AAAA"); $System.out.println (File1.renameto (NewFile ("D:\\CCC"));//Cut and rename the          theFile file2 =NewFile ("Aaa.txt"); the System.out.println (File2.delete ()); theFile File3 =NewFile ("AAA"); -System.out.println (File3.delete ());//Delete unsuccessful because folder AAA has folders inside in     } the  the     /** About      * @throwsIOException the * Create features the      */ the      Public Static voidDemo1 ()throwsIOException { +File File =NewFile ("CCC"); - System.out.println (File.createnewfile ()); the         BayiFile file2=NewFile ("Aaa.txt");//This is also possible to write, the directory name can also have a suffix name the System.out.println (File2.mkdir ()); the          -File File3 =NewFile ("AAA\\CCC"); - System.out.println (File3.mkdirs ()); the     } the  the}

Java's file

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.