How do you go about in creating a direcory folder?

I've been playing around with class File which has the method mkDir() (no arguments and returns type boolean), which i thought would be the answer to making a directory but apparently not, or perhaps i just dont understand it.

12 Years
Discussion Span
Last Post by jwenting

That's indeed the method you want, but most likely you're not calling it correctly.

Without knowing what you're doing and what's happening as a result it's impossible to say more than that.


OK this works, i forgot to add the "\\" at the end of the file argument

import java.io.File;
public class testFile {
	public static void main(String[] args) {
		File file = new File("C:\\tmpFolder\\");
		System.out.println(file.mkdir()? "success" :"failed"); 

also worked out that if you want a relative path i just need to remove the "C:" so it would be like this \\tmpfolder\\ :cheesy:


That's correct. And if you want to create a complete directory path even if parts don't exist, use mkDirs.

Some advise though: don't hardcode path separators. Use the system property "path.separator" instead to make your code platform independent (and of course always use relative paths to some system property).


And if you want to create a complete directory path even if parts don't exist, use mkDirs.

what do you mean by "even if parts don't exist" , can u give us some example, thx


mkDirs will create the entire path you pass it, including non-existent directories.
So if you pass it "/usr/myapp/data/20060407/xml" and only /usr/myapp" exists, it will first create data, then 20060407 under that, and finally xml under that to complete the structure.
mkDir would just give you an error.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.