开发者

Determine whether a folder is present?

How 开发者_C百科would I go about determining whether a file or directory has been created in Java?

I basically want to create a data directory if one is not already present.

Thanks.


You can call File#exists() to determine if it exists, but you can also just call File#mkdirs() to automatically create the whole path if not exist.


I usually use this technique:

    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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜