使用 Java 发现 USB 大容量存储设备

发布于 2024-09-25 05:00:44 字数 516 浏览 8 评论 0原文

背后的故事...

我真的很喜欢电视节目,但我每个月只回家两次。其余时间我住在没有互联网的房子里(虽然离我的大学很近,所以免费的无线网络很棒! - 当它工作时 - )所以我需要一个小软件,能够用我的新节目更新我的便携式硬盘当我回到家时,与播客同步的文件服务器就完成了它的工作。 我使用 Java 完成了它并且它有效。

问题

现在我有一个 .properties 文件,其中存储了该 USB 硬盘的挂载目录。不够。我希望该软件能够发现所有 USB 大容量存储设备,并让用户选择使用哪一个来存储文件。我怎样才能做到这一点?

详细信息

a) 它必须是 Java 语言(我的意思是,它也可以用于执行本地主机命令,例如 dir 或类似的命令)

b) 我的服务器在 Windows 上,但我更喜欢它是一个独立于操作系统的解决方案

The story behind...

I really like TV shows but I go back home only twice a month. The rest of the time I live in a house without internet (close to my university though, so free wifi rocks! - when it works - ) so I needed a little software that was able to update my portable hard-disk with my new shows when I go back home where the file server synchronized with podcasts does its job.
I did it using Java and it works.

The problem

Right now I have a .properties file where I stored the mounted directory of that usb hd. Not enough. I want the software to be able to discover all of USB mass storage devices and let the user select which one use to store files. How can I do that?

Details

a) it has to be in Java (I mean, It could also work with executing local host commands like dir or something like that)

b) my server is on windows, but I prefer it to be an OS independent solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-10-02 05:00:44

虽然我没有在您的问题中看到它,但我认为您非常熟悉 File.listRoots() 方法,该方法返回一个良好的文件根数组。

然后,您可以迭代它们,并尝试识别它们是否是闪存驱动器。有些技巧可能是这样的:

File[] roots = File.listRoots();

if (roots == null) {
  // you have a different problem here.  Is it even a computer we are talking about?
}

// Iterate through roots
for (File root : roots) {
    if (root.canWrite()) {  // Or, you could use File.createTempfile with that folder
        // if root does not contain some well know files
        // Or, if root contains some well known files
        // Based on these 2 hacks, or possible other characteristics, you may be reasonably sure
    }
}

这就是我能提供的。使用更多本机程序可以完成更多工作,然后从 Java 程序调用它们。

While I didn't see it in your question, I presume you are very familiar with File.listRoots() method which returns an array of well, file roots.

Then, you could just iterate over them, and try to identify if they are flash drives. Some hacks may be like:

File[] roots = File.listRoots();

if (roots == null) {
  // you have a different problem here.  Is it even a computer we are talking about?
}

// Iterate through roots
for (File root : roots) {
    if (root.canWrite()) {  // Or, you could use File.createTempfile with that folder
        // if root does not contain some well know files
        // Or, if root contains some well known files
        // Based on these 2 hacks, or possible other characteristics, you may be reasonably sure
    }
}

That's all I can offer. A lot more can be done with more native programs, and then invoking them from the Java program.

灼痛 2024-10-02 05:00:44

您可以检查 javax.usb 项目。
目前有两种经过认证的实现,分别针对 Linux 和 BSD,以及针对 Windows 的实现(因为它看起来,它仍然不完整,但我想它允许您列出连接的 USB 设备)。

但我不确定仅列出 USB 驱动器(而不是 @Amrinder Arora 答案中的所有驱动器)的可能性是否值得采用新的库集并努力实现半完整的实现......

You can check the javax.usb project.
Currently there are two certified implementations, for Linux and BSD, and a implementation for windows (as it seems, its still not complete, but I supose it allows you to list the USB devices conected).

But I'm not sure if the posibility of listing only USB drives (instead all drives like in @Amrinder Arora answer) worth adopt a new library set and struggle with a semi-complete implementation...

潇烟暮雨 2024-10-02 05:00:44

nio中已经提供了一种很漂亮的方式。检查一下:

import java.nio.file.*;
for(Path p:FileSystems.getDefault().getRootDirectories()){
    System.out.println(p);
}

您将获得所有根作为路径对象;

A beautiful way has been provided in nio. Check this out:

import java.nio.file.*;
for(Path p:FileSystems.getDefault().getRootDirectories()){
    System.out.println(p);
}

You'll get All roots as path objects;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文