Create a New folder using Java Program on Windows and Linux machines

后端 未结 4 1996
太阳男子
太阳男子 2020-12-29 05:38

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

相关标签:
4条回答
  • 2020-12-29 05:51
     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());
        }  
    
    • Document
    0 讨论(0)
  • 2020-12-29 06:05

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

    0 讨论(0)
  • 2020-12-29 06:07
    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.

    0 讨论(0)
  • 2020-12-29 06:08
    try{    
         int a,b;
          a=mk.dir();
          b=newfile("\n new.java());
        }  
    
    0 讨论(0)
提交回复
热议问题