1

ファイルまたはディレクトリがJavaで作成されたかどうかを判断するにはどうすればよいですか?

データディレクトリがまだ存在しない場合は、基本的にデータディレクトリを作成したいと思います。

ありがとう。

4

2 に答える 2

4

を呼び出して存在するかどうかを判断できますが、存在しない場合はパス全体を自動的に作成するためにFile#exists()呼び出すこともできます。File#mkdirs()

于 2010-04-25T18:14:48.437 に答える
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
于 2010-04-25T19:27:49.687 に答える