找到FileStore的目录 [英] Find the directory for a FileStore
问题描述
我正试图找到一种方法来检测闪存驱动器何时插入我的计算机。到目前为止,我发现的解决方案是投票更改的 FileSystem#getFileStores
。这确实告诉我何时插入了闪存驱动器,但据我所知,没有办法检索它的位置。 FileStore #type
和 FileStore #name
两者看起来都非常不可靠,因为它们的返回值是特定于实现的,但它们似乎是唯一可能返回任何可能有用的相关信息的方法找到 FileStore
的目录。
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 does indeed tell me when the flash drive has been inserted, but as far as I can tell there is no way to retrieve the location for it. FileStore#type
and FileStore#name
both seem highly unreliable as their return value is implementation specific, but they appear to be the only methods that might return any relevant information that might help find the directory for the FileStore
.
考虑到这一点,请输入以下代码:
With that in mind, the following code:
public class Test {
public static void main(String[] args) throws IOException {
for (FileStore store : FileSystems.getDefault().getFileStores()) {
System.out.println(store);
System.out.println("\t" + store.name());
System.out.println("\t" + store.type());
System.out.println();
}
}
}
给我这个输出:
/ (/dev/sda5)
/dev/sda5
ext4
/* snip */
/media/TI103426W0D (/dev/sda2)
/dev/sda2
fuseblk
/media/flashdrive (/dev/sdb1)
/dev/sdb1
vfat
当它转过来out, FileStore #type
返回驱动器的格式, FileStore #name
返回设备文件的位置驾驶。据我所知,唯一具有驱动器位置的方法是 toString
方法,但从中提取路径名称似乎很危险,因为我不是确定特定解决方案在其他操作系统和未来版本的Java上的表现如何。
As it turns out, FileStore#type
returns the format of the drive and FileStore#name
returns the location of the device file for the drive. As far as I can tell, the only method which has the location of the drive is the toString
method, but extracting the path name out of it seems dangerous because I'm not sure how well that particular solution would hold up on other operating systems and future versions of Java.
这里有什么东西我不知道或者这是不可能纯粹的Java?
Is there something I'm missing here or is this simply not possible purely with Java?
系统信息:
$ java -version
java version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu2)
OpenJDK Client VM (build 22.0-b10, mixed mode, sharing)
$ uname -a
Linux jeffrey-pc 3.2.0-24-generic-pae #37-Ubuntu SMP Wed Apr 25 10:47:59 UTC 2012 i686 athlon i386 GNU/Linux
推荐答案
这是一个临时工作,直到更好找到解决方案:
Here's a temporary work around until a better solution is found:
public Path getRootPath(FileStore fs) throws IOException {
Path media = Paths.get("/media");
if (media.isAbsolute() && Files.exists(media)) { // Linux
try (DirectoryStream<Path> stream = Files.newDirectoryStream(media)) {
for (Path p : stream) {
if (Files.getFileStore(p).equals(fs)) {
return p;
}
}
}
} else { // Windows
IOException ex = null;
for (Path p : FileSystems.getDefault().getRootDirectories()) {
try {
if (Files.getFileStore(p).equals(fs)) {
return p;
}
} catch (IOException e) {
ex = e;
}
}
if (ex != null) {
throw ex;
}
}
return null;
}
据我所知,此解决方案仅适用于Windows和Linux系统。
As far as I know, this solution will only work for Windows and Linux systems.
你必须在Windows循环中捕获 IOException
因为如果CD驱动器中没有CD异常当您尝试为其检索 FileStore
时抛出此异常。这可能在您遍历每个根之前发生。
You have to catch the IOException
in the Windows loop because if there is no CD in the CD drive an exception is thrown when you try to retrieve the FileStore
for it. This might happen before you iterate over every root.
这篇关于找到FileStore的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!