2010-04-25 2 views

ответ

4

Вы можете позвонить по телефону File#exists(), чтобы узнать, существует ли он, но вы также можете просто позвонить File#mkdirs(), чтобы автоматически создать весь путь, если он не существует.

1

Я обычно использую этот метод:

File folderLocation = new File("/blah/blah/mysystem/myfolder"); 

    if (folderLocation.exists()) { 
     if (!folderLocation .isDirectory()) { 
      throw new IOException("File-system item with path [" + folderLocation.getAbsolutePath() + "] exists but is not a folder."); 
     }     
    } else { 
     if (!folderLocation.mkdirs()) { 
      throw new IOException("Could not create folder with path : " + folderLocation.getAbsolutePath()); 
     } 
    } 

    // we are guaranteed that the folder exists here