Java judges whether a folder or file exists, otherwise creates a folder

1. Determine whether the file exists and create a file that does not exist

File file =new File("C:\\Users\\QPING\\Desktop\\JavaScript");    
//create the folder if it doesn't exist    
if  (!file .exists()  && !file .isDirectory())      
{       
    System.out.println("// does not exist");  
    file .mkdir();    
} else   
{  
    System.out.println("//Directory exists");  
}

 

 

  2. Determine whether the folder exists, and create a folder if it does not exist

File file=new File("C:\\Users\\QPING\\Desktop\\JavaScript\\2.htm");    
if(!file.exists())    
{    
    try {    
        file.createNewFile();    
    } catch (IOException e) {    
        // TODO Auto-generated catch block    
        e.printStackTrace ();    
    }    
}  

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987871&siteId=291194637