The difference between mkdir () and Mkdirs () in the Java.io.File class __java.io.file.mkdir

Source: Internet
Author: User
Tags parent directory


Working with files in Java often uses the Java.io.File.mkdir () and Java.io.File.mkdirs () two methods to create a directory (folder), which is very similar, with no parameters and the return value is Boolean-type , which are used to create a directory for the specified path name, the main differences are as follows:



Java.io.File.mkdir (): Only one level directory can be created, and the parent directory must exist, otherwise a directory cannot be created successfully.



Java.io.File.mkdirs (): You can create multilevel directories, and the parent directory does not necessarily exist.



Here is a routine that can profoundly appreciate the difference between the two:





<span style = "font-size: 18px;"> import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/ **
 * mkdir and mkdirs test routines
 * Time: 2016-5-15 22:34
 * Description: 1. java.io.File.mkdir () and java.io.File.mkdirs () are used to create the directory corresponding to the specified path name.
 * But java.io.File.mkdir () can only create a first-level directory and the parent directory must exist, otherwise a directory will not be created correctly;
 * java.io.File.mkdir () can create multi-level directories, the parent directory does not necessarily exist.
 * * /

public class mkdirsAndmkdirTestMain {
private static final String path1 = "path1";
private static final String path2 = "path2 / path2 /";
private static final String path3 = "path3";
private static final String path4 = "path4 / path4 /";
private static final String filePath1 = "file1.txt";
/ **
* @param args
* /
public static void main (String [] args) {
The
File file1 = new File (path1);
if (! file1.exists ()) {
file1.mkdir ();
}
System.out.println (file1.getPath ());
The
File file2 = new File (path2);
if (! file2.exists ()) {
file2.mkdir ();
}
System.out.println (file2.getPath ());
The
File file3 = new File (path3);
if (! file3.exists ()) {
file3.mkdirs ();
}
System.out.println (file3.getPath ());

File file4 = new File (path4);
if (! file4.exists ()) {
file4.mkdirs ();
}
System.out.println (file4.getPath ());
The
File file4_1 = new File (path4 + filePath1);
if (! file4_1.exists ()) {
try {
file4_1.createNewFile ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}
System.out.println (file4_1.getPath ());
The
try {
PrintWriter pw = new PrintWriter (new BufferedWriter (new FileWriter (file4_1)));
long current = System.currentTimeMillis ();
String time = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"). Format (new Date (current));
pw.write ("Time:" + time);
pw.println ();
pw.println ("Absolute path of current file:" + file4_1.getAbsolutePath ());
pw.write ("Relative path of current file:" + file4_1.getPath ());
pw.close ();
The
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
The
}

}
</ span>




















Path1 is a first-level path, using Java.io.File.mkdir () to successfully create the corresponding directory;



And Path2 is a two-level path, using Java.io.File.mkdir () does not create the corresponding directory;



Path3 is a first-level path, Path4 is a two-level path, each using Java.io.File.mkdirs () can successfully create their respective directories.



Test example Routines Code (github)


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.