How do I create folders in Java?

Asked 1 years ago, Updated 1 years ago, 121 views

I'd like to create a folder named "New Folder". If there's a folder called "New Folder" already, just ignore it I want to create a new folder, what should I do?

java directory

2022-09-22 22:20

1 Answers

File theDir = new File ("New Folder");

// If the directory does not exist, create the directory.
if (!theDir.exists()) {
    System.out.println("creating directory: " + directoryName);
    boolean result = false;

    try{
        theDir.mkdir();
        result = true;
    } 
    catch(SecurityException se){
        //handle it
    }        
    if(result) {    
        System.out.println("DIR created");  
    }
}

You can do it like this.


2022-09-22 22:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.