Let's look at the specific code first:
import java.io.File;import java.io.IOException;public class CreatFile{public static void main(String[] args) {File newDir=new File("D:/test"); //Declare the disk directory File newFile = new File(newDir,"test.txt"); //Declare the directory file boolean newCreatDir=newDir.exists(); boolean newCreatFile=newFile.exists();//Create the directory and files if(newCreatDir==false){try{ //Exception listening newDir.mkdirs(); //Create directory System.out.println(newDir.getAbsolutePath()+"Directed"); newFile.createNewFile(); //Create file System.out.println(newFile.getAbsolutePath()+"File created");}catch(IOException e){ //Catch exception e.printStackTrace(); //Print the exception information on the command line where the error occurred in the program and the reason }}else{System.out.println(newDir.getAbsolutePath()+"Direct already exist");}if(newCreatFile==true){System.out.println(newFile.getAbsolutePath()+"File already exists");}}}}illustrate:
The method of creating a directory, mkdirs(); or mkdir(); The difference is that mkdirs() can be created in multiple levels.
Create file method, createNewFile();