Find the directory for a FileStore

前端 未结 4 1260
清歌不尽
清歌不尽 2020-12-16 00:21

I\'m trying to find a way to detect when a flash drive has been plugged into my computer. So far, the solution I found was to poll FileSystem#getFileStores for changes. This

4条回答
  •  借酒劲吻你
    2020-12-16 00:57

    This works for Windows:

        public Path getFileStoreRootPath(FileStore fs) throws Exception {
        for (Path root : FileSystems.getDefault().getRootDirectories()) {
            if (Files.isDirectory(root) && Files.getFileStore(root).equals(fs)) {
                return root;
            }
        }
    
        throw new RuntimeException("Root directory for filestore " + fs + " not found");
    }
    

    Basically, by filtering by condition Files.isDirectory(root) we are excluding all CD/DVD drives which will throw IOException when compact-disc is not inserted.

提交回复
热议问题