0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

並行処理時のFileAlreadyExistsException

Last updated at Posted at 2022-01-30

テスト中、起こらないはずが時たまエラーになって確認したもの

参照したサイト:

問題になるコード

if(!directory.exsits()){
 Files.createDirectory(dir);
}

FileAlreadyExistsException原因→並行処理による

パスの存否によってパスを作成するように対応したはずなのに。。。!

あ。。並行処理を見逃した。
たとえば、処理A、Bがあれば
A:check to path exist -> nope -> so go next step
B:also check to path exist -> nope( have not made by A)
A:make dir
B:also make dir ☆ throw FileAlreadyExistsException
( ノД`)シクシク…つまり、並行処理をやめるか、フォルダが存在しても作ったら問題がように修正が必要だった。

開発方法

あ。。。簡単すぎるが

//修正前
Files.createDirectory(dir);
//修正後
Files.createDirectories(dir);

createDirectoriesに修正したら問題はなくなった。
元々このメソッドは dir = a/b/c/d の場合、 aからdまで一通り作ってくれるメソッドでちょっと。。。?違くない。。?とするけど、やりたいものはフォルダがなければ作るのはあったので採用!

※存在しないすべての親ディレクトリをまず作成することで、ディレクトリを作成します。createDirectory メソッドとは異なり、ディレクトリがすでに存在しているために作成できなかった場合でも例外はスローされません。

退屈で調査したもの 심심해서 뜯어본 코드

 public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
        throws IOException
    {
        provider(dir).createDirectory(dir, attrs);
        return dir;
    }

내부적으로 IOException이 나타나면 그냥 예외로 에러를 내게끔 되어있다.
[IOException_FileAlreadyExistsException]:https://docs.oracle.com/javase/jp/7/api/java/nio/file/FileAlreadyExistsException.html

image.png

 public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
        throws IOException
    {
        provider(dir).createDirectory(dir, attrs);
        return dir;
    }

attrs? 속성은 뭔가 바꿀 수 있는 건가...

public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
        throws IOException
    {
        // attempt to create the directory
        try {
            createAndCheckIsDirectory(dir, attrs);
            return dir;
        } catch (FileAlreadyExistsException x) {
            // file exists and is not a directory
            throw x;
        } catch (IOException x) {
            // parent may not exist or other reason
        }

~~省略~~

and おまけ
https://stackoverflow.com/questions/49235657/mkdirs-vs-files-createdirectoryies ふむふむ

The difference seems to come down to error handling and what you want to do with the code. For example, mkdir throws an exception only if you don't have permission to create the folders, while createDirectory will throw other exceptions, including if a file exists with the same name as the directory you're trying to create. mkdir is also not atomic, so if it fails to create a folder, it might leave behind all the folders it did create up to that point, while createDirectory would leave no folders if it failed

  1. java.io.File java1からあったpacakge<-legacy
  2. java.nio.file.Path version7からあった
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?