質問

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

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

ありがとう。

役に立ちましたか?

解決

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

他のヒント

私は通常、この手法を使用します:

    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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top