How to handle FileSystemAlreadyExistsException?
Asked Answered
S

2

13

How should you handle a FileSystemAlreadyExistsException when calling FileSystems.newFileSystem?

One way would be to use the already created FileSystem (getFileSystem​), but then you could run into problems when the original creator closes it while you are still using it.

In case there is no generic answer to this question, then what about ZipFileSystems? Let's say I want to create one for a zip file and I do not know and cannot control whether a FileSystem already exists for this specific zip file.

Is there a reliable way to handle a FileSystemAlreadyExistsException?

Speiss answered 28/4, 2019 at 16:37 Comment(2)
You mean that within the same JVM you have no control over who is using which Zip file?Dinitrobenzene
Yes, even though it is unlikely, but I want to protect against the case where for example a third-party library I am using might create a file system for the same resource.Speiss
P
2

Sorry for being late here, but I just ran into this myself. There are several versions of the FileSystems.newFileSystem() call, some versions throw FileSystemAlreadyExistsException, others do not. I found that using the version that accepts a Path object and did not throw the Exception, that allowed me to work around the issue I was having.

Parsley answered 11/10, 2022 at 15:19 Comment(1)
I don't think this completely answers this question, but it is useful nonetheless! There is one potential issue though: These methods seem to always create new file system instances. This defeats any concurrency guarantees made by FileSystem; modifications made to the file system might get lost, at least for ZipFileSystem.Speiss
K
0

Try putting a try/catch block around the code that is causing the exception.

Killebrew answered 17/2, 2022 at 15:55 Comment(1)
Hi, Zachary - the answer you gave links to a lengthy text, and the relation to the question is not immediately obvious. The requester might TLDR, because they think you just want the text to be read. If you have understood that linked text, why no give a short excerpt, at least additionally to the link?Visor

© 2022 - 2024 — McMap. All rights reserved.