Java basics [12] java. io. file (folder) creation, javajava. io. file
Using java. io. file to create a file (folder) is the most basic knowledge of java.
Jdk api description:
You can see the following code:
File file1 = new File ("F:/AAA/BBB/CCC"); if (file1.mkdirs () {System. out. println ("the multilevel folder is created successfully! The created File directory is: "+ file1.getPath () +", and the parent File is: "+ file1.getParent ();} File file2 = new File (" F: /AAA/BBB/CCC/DDD "); if (file2.mkdir () {System. out. println ("single folder DDD created successfully! The created File directory is: "+ file2.getPath () +", and the parent File is: "+ file2.getParent ();} File file3 = new File (" F: /AAA/BBB/CCC/DDD "," mytest.txt "); try {if (file3.createNewFile () {System. out. println ("the file under the multilevel folder is created successfully! The created file is: "+ file3.getAbsolutePath () +", and the parent file is: "+ file3.getParent () ;}} catch (IOException e) {e. printStackTrace ();}
Output:
This makes it clear that mkdir () creates a single folder and ensures that its parent folder exists.
Mkdirs () creates multiple folders without the need to ensure that their parent folders exist.
Output the file to a folder to ensure that the target folder exists.