Basic File class, file class

Source: Internet
Author: User

Basic File class, file class

The role of the File class:
The Java io package defines the File class for managing files or folders.
The File class can only be used to indicate the information (attributes) of a File or folder and delete and create a File or folder.
(Content cannot be accessed)
By creating a File object, you can use a program to operate files or folders on your computer.

 

File class construction method:
File (String path): Creates a new File object using the specified path name String. The abstract path name is used to locate the path of a File or folder.
File (File parent, String path): Creates a new File object based on the parent File object and path sub-File name String.

 

Common APIs of the File class
File Operations:
Exists (): determines whether a file exists. The returned value is a Boolean value.
GetCanonicalPath (): Get the standard path name of the current file
Separator: Used to indicate path-level delimiters, which can avoid inconsistency between operators in different operating systems.
CreateNewFile (): create a file in the hard disk
Length (): length
LastModified (): Creation Time: new Date (File. lastModified ())
Delete (): delete
GetName (): Get the file or folder name

Folder operation:
Exists (): determines whether a file exists. The returned value is a Boolean value.
GetCanonicalPath (): Get the standard path name of the current file
Separator: Used to indicate path-level delimiters, which can avoid inconsistency between operators in different operating systems.
Boolean mkdir (): Create a folder
Mkdirs (): Creates a folder if the parent folder does not exist.
ListFiles (): the path name returned by this method is used to represent the array of files and directories in the directory of this abstract path name.
ListFiles (FileFilter filter): the path name returned by this method is used to represent an array of files and directories in the directory of this abstract path name, and contains a filter of the FileFilter type.
IsDirectory (): determines whether a folder is used.
DelDirectory (pp): Call the delete folder Method

1 public void testFile01 () throws IOException {2 // create a File object in the computer memory to correspond to the current folder 3 File = new file (". "); 4 System. out. println ("whether the current folder exists:" + file. exists (); 5 System. out. println ("view the path of the current folder:" + file. getCanonicalPath (); 6 // there is no direct connection with the folder in the hard disk, but there is only an object in JVM 7 File fileDemo = new File (file, "fileDemo"); 8 System. out. println ("check whether the fileDemo folder exists in the current folder:" + fileDemo. exists (); 9 if (! FileDemo. exists () {10 fileDemo. mkdir (); // create the fileDemo folder 11} 12 13 File testFile = new File (file, "aa" + File. separator + "testFile"); 14 if (! TestFile. exists () {15 // boolean flag = testFile. mkdir (); 16 // System. out. println ("creation result:" + flag); 17 testFile. mkdirs (); // create a folder. When the parent directory does not exist, the parent directory will be created first 18} 19 20 File aFile = new File (fileDemo, "a.txt"); 21 System. out. println ("whether a.txt exists in the hard disk:" + aFile. exists (); 22 if (! AFile. exists () {23 aFile. createNewFile (); // create a file on the hard disk 24} 25 System. out. println ("whether a.txt exists in the hard disk:" + aFile. exists (); 26 System. out. println ("a.txt standard path:" + aFile. getCanonicalPath (); 27 System. out. println ("a.txt file length:" + aFile. length (); 28 System. out. println ("a.txt creation time:" + new Date (aFile. lastModified (); 29/* 30 * delete a file or folder represented by a file object. If yes, true is returned. 31 * if the object represents a folder, you must ensure that the folder is empty before deleting 32 */33 aFile. delete (); 34 // failure to delete aa 35 File aa = new File (file, "aa"); 36 aa. delete (); 37}
Public void testFile02 () {File file = new File (". "); // current folder object File [] files = null; if (file. exists () {// listFiles (): view the subfolders in the current folder. If the folder path is empty, return null // if the path name does not indicate a folder or an I/O exception occurs, null files = file is returned. listFiles () ;}for (File f: files) {// getName (): Get the File or folder name System. out. println (f. getName ());}}
Public void testFile03 () throws IOException {File path = new File (". "); File pppp = new File (path," pp "+ File. separator + "ppp" + File. separator + "pppp"); File demoFile = new File (pppp, "demo.txt"); if (! Pppp. exists () {pppp. mkdirs (); // create the parent folder with the subfolders} if (! DemoFile. exists () {demoFile. createNewFile (); // create a File} File pp = new File (path, "pp"); delDirectory (pp); // call the delete folder method. If there is no File under it, it can be successful}
// Use the Traversal method to delete a folder. if there are files in the folder, delete the File to prevent the folder from being deleted. public static void delDirectory (File dir) throws IOException {if (! Dir. exists () {// if the folder does not exist, throw the runtime exception throw new RuntimeException ("folder" + dir + "");} if (! Dir. isDirectory () {// determines whether the folder throw new RuntimeException (dir + "not a directory");} File [] subs = dir. listFiles (); if (subs! = Null & subs. length> 0) {// traverses all sub-files and folders and deletes the contents. // traverses all subs for (File f: subs) {if (! F. isDirectory () {// either a folder or a file System. out. println ("file name:" + f. getName (); if (! F. delete () {// If the deletion fails, an exception is thrown. If the deletion succeeds, the system does not come in, and throw new IOException ("file cannot be deleted" + f. getName () ;}} else {// folder processing System. out. println ("Start processing Folders" + f. getName (); delDirectory (f); // call the delete folder method to delete the sub-Folder System. out. println ("The subfolder has been deleted. Start to delete this folder" + f. getName (); f. delete (); // After deleting the sub-files and sub-folders, You need to delete the current folder} dir. delete (); // delete the contents of the dir directory}
// Use the filter to filter out some files and process the public void testFile04 () {File file = new File ("src" + File. separator + "day07"); // create the file object FileFilter filter = new FileFilter () {// This method is used to define filter rules, if return true, it indicates that the returned value meets the rule is false. If return is false, it indicates that the @ Override public boolean accept (File pathname) does not comply with the rule) {// matches the content that complies with the regular expression rules if (pathname. getName (). matches (". * [.] java ") {return true;} return false ;}; // pass in the filter object and filter out the file object that does not comply with the rule. File [] paths = file. listFiles (filter); for (File f: paths) {System. out. println (f. getName ());}}

 

Related Article

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.