15

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 }; } 
8
  • possible duplicate of How to create a directory in Java? Commented Jan 11, 2014 at 6:03
  • Thats where I got the code from but it doesn't work for me... Commented Jan 11, 2014 at 6:04
  • 2
    You have syntax error, File SimpleHTML = new File("C:/Users/"+user+"/Documents"); { , add those plus to concat 2 string Commented Jan 11, 2014 at 6: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. Commented Jan 11, 2014 at 6:08
  • @Zeak, if you want to create the folder SimpleHTML then it should be File SimpleHTML = new File("C:/Users/"+user+"/Documents/SimpleHTML"); Commented Jan 11, 2014 at 6:11

2 Answers 2

36

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 ;)

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
sorry I don't know what you mean, as I sure you are aware I'm quite new to Java.
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
Makes a nice change (to get something that works) ;)
8

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

File userDirectory = org.apache.commons.io.FileUtils.getUserDirectory(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.