21

How can I create a Folder using Java code on both Windows and Linux machines?

0

4 Answers 4

42
new File("/path/to/folder").mkdir();

If you want to created nested folders (i.e. more than one folder on the path may be missing), then use mkdirs(). See java.io.File.

Note that forward slashes would normally not work on windows, but Java normalizes the path and translates forward to backward slashes.

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

2 Comments

It is not working for me . Can any one let me know the reason ? I have posted this already , but still couldn't able to resolve it stackoverflow.com/questions/18940987/…
reply For above comment - Resolved the issue : Instead of giving path as " /xyz.com/appFolder/ " , should give full path as "home/XYZ/public_html/appFolder"
4
 try{
    String strDirectoy ="test";
    String strManyDirectories="dir1"+File.Separator+"dir2"+File.Separator+"dir3";

    // Create one directory
    boolean success = (new File(strDirectoy)).mkdir();
    if (success) {
      System.out.println("Directory: " + strDirectoy + " created");
    }    


    // Create multiple directories
    success = (new File(strManyDirectories)).mkdirs();
    if (success) {
      System.out.println("Directories: " + strManyDirectories + " created");
    }

    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }  

Comments

2

Use File.mkdir() (http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdir())

Comments

-2
try{    
     int a,b;
      a=mk.dir();
      b=newfile("\n new.java());
    }  

1 Comment

Please add some details, at least catch block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.