Java: How to create a new folder in Mac OS X

家住魔仙堡 提交于 2019-12-11 12:14:58

问题


I want to create a folder in ex. my desktop in Mac OS X

I try to use this code, instead of Mymac is my name of course:)

String path="/Users/Mymac/Desktop";
String house = "My_home";
File file=new File(path);
if(!file.exists())
    file.mkdirs(); // or file.mkdir()

file=new File(path + "/" + house);   
try {
  if(file.createNewFile())
   {
   }
} catch (IOException ex) {

  ex.printStackTrace();
}

Do you know how I could create a new folder? And another thing is when I want to create a folder in the directory where my code is, do you know how I could write that? I have tried

   String path="./";

   String path="::MyVolume";

   String path=".";

回答1:


To find the folder/directory where one of your classes is (assuming they are not in a Jar), and then to create a subfolder there:

    String resource = MyClass.class.getName().replace(".", File.separator) + ".class";      
    URL fileURL = ClassLoader.getSystemClassLoader().getResource(resource);

    String path = new File(fileURL.toURI()).getParent();
    String mySubFolder = "subFolder";
    File newDir = new File(path + File.separator + mySubFolder);
    boolean success = newDir.mkdir();

(The code above could be made more compact, I listed it more verbosely to demonstrate all the steps.) Of course, you need to be concerned about permission issues. Make sure that the user which is running java has permission to create a new folder.




回答2:


A platform-independent way:

File rootDir = File.listRoots()[0];
File dir = new File(new File(new File(rootDir, "Users"), "Mymac"), "Desktop");
if (!dir.exists()){
    dir.mkdirs();
}



回答3:


Your code is ok and will work. Perhaps you have a typo in your username in your file-path ("Mymac"), so you don't see the changes, since they go to another folder.

Running this code on my machine works fine and gives the expected result.

To make your code platform-independant, you can build your file-path with the following trick:

File path = new File(File.listRoots()[0], "Users" + System.getProperty("file.separator") + "Mymac" + System.getProperty("file.separator") + "Desktop"));

If "My_home" should be a folder and not a file, you have to change the file.createNewFile() - command. More detailed information you'll find in the answer of Thomas.




回答4:


Use file.mkdir() or file.mkdirs() instead of file.createNewFile() and it should work, if you have permission to create new folders and files.

mkdirs() will create the subfolders if they don't exist, mkdir() won't.

To create a directory in your base directory (the one you start your application from), just provide a relative path name: new File("mydir").mkdir();

Edit: to make file handling easier, I'd suggest you also have a look at Apache Commons' FilenameUtils and FileUtils.



来源:https://stackoverflow.com/questions/8387581/java-how-to-create-a-new-folder-in-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!