14

Hello I was just wondering how to make a custom directory below the current user's home directory. I've tried this already and it doesn't work... (Code below)

I want it to go to this directory and create the folder in the documents folder

c:/users/"user"/documents/SimpleHTML/

File SimpleHTML = new File("C:/Users/"user"/Documents"); {

//  if the directory does not exist, create it
if (!SimpleHTML.exists()) {
    System.out.println("createing direcotry: " + SimpleHTML);
    boolean result = SimpleHTML.mkdir();

        if(result) {
            System.out.println("Direcotry created!");
        }
}

new simplehtmlEditor() {
    //Calling to Open the Editor
};

}
Shog9
  • 156,901
  • 35
  • 231
  • 235
ayeama
  • 173
  • 1
  • 2
  • 11
  • possible duplicate of [How to create a directory in Java?](http://stackoverflow.com/questions/3634853/how-to-create-a-directory-in-java) – jmj Jan 11 '14 at 06:03
  • Thats where I got the code from but it doesn't work for me... – ayeama Jan 11 '14 at 06:04
  • 2
    You have syntax error, `File SimpleHTML = new File("C:/Users/"+user+"/Documents"); {` , add those plus to concat 2 string – jmj Jan 11 '14 at 06:05
  • I've just changed the code and it still does not create the folder, even the result println doesn't get printed to the console. – ayeama Jan 11 '14 at 06:08
  • @Zeak, if you want to create the folder SimpleHTML then it should be File SimpleHTML = new File("C:/Users/"+user+"/Documents/SimpleHTML"); – sasankad Jan 11 '14 at 06:11
  • @sasankad that gives me an error with the "+user+" part... user cannot be resolved to a variable – ayeama Jan 11 '14 at 06:20
  • What is the value of the String variable "user" in your example ? Please check while running. Then go ahead and check to see if c:/Users/ (insert your variable value) exists. Respond here to tell the results. There is a lot more to file i/o than simple syntax, the context of your existing file structure really matters – OYRM Jan 11 '14 at 06:22
  • I don't have a String for "user" I should have told you this, I would like it so that when other people use this program that it goes to their documents folder. – ayeama Jan 11 '14 at 06:25

2 Answers2

35

First, use System.getProperty("user.home") to get the "user" directory...

String path = System.getProperty("user.home") + File.separator + "Documents";
File customDir = new File(path);

Second, use File#mkdirs instead of File#mkdir to ensure the entire path is created, as mkdir assumes that only the last element needs to be created

Now you can use File#exists to check if the abstract path exists and if it doesn't File#mkdirs to make ALL the parts of the path (ignoring those that do), for example...

if (customDir.exists() || customDir.mkdirs()) {
    // Path either exists or was created
} else {
    // The path could not be created for some reason
}

Updated

A simple break down of the various checks that might need to be made. The previous example only cares if the path exists OR it can be created. This breaks those checks down so that you can see what's happening...

String path = System.getProperty("user.home") + File.separator + "Documents";
path += File.separator + "Your Custom Folder"
File customDir = new File(path);

if (customDir.exists()) {
    System.out.println(customDir + " already exists");
} else if (customDir.mkdirs()) {
    System.out.println(customDir + " was created");
} else {
    System.out.println(customDir + " was not created");
}

Note, I've added an additional folder called Your Custom Folder to the path ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I just tested that and put a simple System.out.println to see what would happen and on console it says that it worked but I cannot see a folder in Documents. – ayeama Jan 11 '14 at 06:35
  • Can you check to see of the File#exists is returning true or not, as I would imagine that Documents already exist...I'll update the question a little – MadProgrammer Jan 11 '14 at 06:37
  • sorry I don't know what you mean, as I sure you are aware I'm quite new to Java. – ayeama Jan 11 '14 at 06:39
  • Yes that's it Thank you MadProgramer! it was created in the spot that I want and you helped me a lot, And Now I can continue making my program :) also now I know how to do directorys – ayeama Jan 11 '14 at 06:52
  • Makes a nice change (to get something that works) ;) – MadProgrammer Jan 11 '14 at 06:53
8

Note that you can use Commons-IO for this, too:

File userDirectory = org.apache.commons.io.FileUtils.getUserDirectory();
Tom
  • 1,965
  • 3
  • 25
  • 33