GetParentFile () method of the File class in java

Source: Internet
Author: User
Tags abstract parent directory

GetParentFile () is used to obtain the parent directory.

The problem is. mkdirs (); this method can only generate one layer of folders, but cannot generate files, and your file object path is directly to the file layer, without getParentFile () to obtain the parent directory, as you said, two folders are generated instead of the files you want. Therefore, you must first call getParentFile () to obtain the parent directory. mkdirs () is used to generate the parent directory folder, and finally generate the file you want under this folder, which is the desired result.

The java. io. File. getParentFile () method returns the parent abstract path name of this abstract path name, or if this path name does not specify the parent directory, it is null.

The following declaration is the java. io. File. getParentFile () method:

Public File getParentFile () parameter • NA
Return value this method returns the abstract path name of the parent directory of this abstract path name, or null if the path name does not specify a parent.

Exception • NA

Example


String fileName = "new. txt ";
String dir = "mydir1/mydir2 ";
File f = new File (dir, fileName );
When I directly
F. mkdirs ();
New. txt is also changed to a directory.
F. getParentFile (). mkdirs ();
Only the directory of mydir1/mydir2 is available.

Example

The following example demonstrates the usage of java. io. File. getParentFile.

Package com. yiibai;

Import java. io. File;

Public class FileDemo {
Public static void main (String [] args ){
     
File f = null;
File f1 = null;
String v;
Boolean bool = false;
     
Try {
// Create new file
F = new File ("C: \ test.txt ");
        
// Returns abstract parent pathname
F1 = f. getParentFile ();
        
// Absolute path from abstract pathname
V = f1.getAbsolutePath ();
        
// True if the file path exists
Bool = f. exists ();
        
// If file exists
If (bool)
         {
// Prints
System. out. print ("Parent file path:" + v );
         }
} Catch (Exception e ){
// If any error occurs
E. printStackTrace ();
      }
   }
}

Let's compile and run the above program, which will produce the following results:

Parent file path: C:

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.