Java File class provides methods to create directory. Two important methods are mkdir and mkdirs. It is important to understand these two methods and pay attention to the return values to confirm that the operation is successful.
Method mkdir()
Creates the directory named by this abstract pathname. The boolean return value indicates whether the operation is successful.
Method mkdirs()
This is a very useful method as it can create parent directories if not existing. It can create directory named by the abstract path name supplied to the method, including any necessary but nonexistent parent directories. Here again pay attention to the returned Boolean value to confirm that the operation is successful.
import java.io.File; public class JavaFile { public static void main(String[] args) throws Exception{ File f= new File("E:/helloworld/t/"); boolean status=f.mkdirs(); if(status){ System.out.println("Directory(s) created"); }else{ System.out.println("Directory creation failed"); } } }
The post How to create a directory in java appeared first on Java Hash.