如何在 Java 中弹出卷?

发布于 2024-07-25 08:31:16 字数 142 浏览 2 评论 0原文

如何使用 Java 跨平台“弹出”卷?

我有一个程序可以在可移动驱动器(USB 存储卡读卡器)上执行一些操作,一旦完成,我希望该程序弹出/卸载/删除(取决于我们正在谈论的操作系统术语)存储卡。

有没有可靠的跨平台方法来做到这一点?

How can I "eject" a volume with Java, cross platform?

I have a program that does some operations on a removable drive (USB memory card reader), and once it's done, I want the program to eject/unmount/remove (depending which os lingo we're talking in) the memory card.

Is there a reliable cross-platform method of doing this?

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

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

发布评论

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

评论(2

葬心 2024-08-01 08:31:16

可能不是您正在寻找的答案,但是...

不。

据我所知,还没有一种既定的单一平台方法可以做到这一点。 就此而言,我从未遇到过 Java 方法来做到这一点。 相当可怕的 C# CodeProject 确实允许弹出设备,但仅限于 Windows。

各种令人沮丧的是,Java USB 库甚至没有提示弹出设备。 它们不能在所有平台上运行,所以即使它们可以运行,也对您没有帮助。

我的建议:为每个平台启动一些脚本或可执行文件,然后根据需要启动一个进程。

Probably isn't the answer you're looking for, but...

No.

To my knowledge, there isn't an established single-platform way of doing this. For that matter, I've never come across a Java way of doing this. A rather scary C# CodeProject does allow ejecting devices, but only on Windows.

The various, depressingly poor, Java USB libraries don't even hint at ejecting devices. They don't work across all platforms, so even if they did it wouldn't help you.

My suggestion: gin up some scripts or executables for each platform, and then just spin up a Process as needed.

放血 2024-08-01 08:31:16

回复有点晚,但我认为值得分享......
由于默认的 Java API 不具备此功能,因此您可以使用上面提到的外部库,但是我个人发现在 jar 的类路径中包含第三方 exe 文件更方便(对于 Windows),将其提取到临时文件夹,在需要时执行它,然后在应用程序完成后将其删除。
作为第三方程序,我使用 this 这是一个仅 CLI 的程序,可以执行连接设备的一些技巧,然后使用此代码:

FileUtils.copyInputStreamToFile(MyClass.class.getClassLoader().getResourceAsStream(program),TEMP_EJECT_PROGRAM);

将其导出到临时文件位置(使用 ApacheIO,您绝对可以不使用它),以及此代码:

    private void safelyRemoveDrive(final String driveLetter) {
    new Thread(new Runnable() {
        public void run() {
            if (TEMP_EJECT_PROGRAM.exists()) {
                System.out.println("Removing " + driveLetter);
                try {
                    Process p = Runtime.getRuntime()
                            .exec("\"" + TEMP_EJECT_PROGRAM.toString() + "\" " + driveLetter + " -L");
                    p.waitFor();
                    Scanner s = new Scanner(p.getInputStream());
                    while (s.hasNextLine())
                        System.out.println(s.nextLine());
                    s.close();
                    System.out.println("Removed " + driveLetter + ".");
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

删除驱动器。 上面的代码肯定不适合所有应用程序,特别是第二个代码并不是最好的,还有其他比生成匿名线程更好的方法......但是您仍然了解它背后的想法:)

最后,我建议您在用户的机器上执行任何第三方软件之前适当地通知用户并询问他们的同意...

我希望这有帮助:-)

A litle late response but i thought it was worth sharing...
Since the default Java API does not come with this feature on it, you could use external libraries as mentioned above, however i personally found it much more convenient (for windows) to have a third party exe file in the jar's classpath, extract it in the temp folder, execute it when needed and then remove it once the aplication is done with it.
As a third party program i used this which is a CLI only program that can do a few tricks with connected devices, and then used this code:

FileUtils.copyInputStreamToFile(MyClass.class.getClassLoader().getResourceAsStream(program),TEMP_EJECT_PROGRAM);

to export it to the temp file location (Using ApacheIO, you can definately do without it), and this code:

    private void safelyRemoveDrive(final String driveLetter) {
    new Thread(new Runnable() {
        public void run() {
            if (TEMP_EJECT_PROGRAM.exists()) {
                System.out.println("Removing " + driveLetter);
                try {
                    Process p = Runtime.getRuntime()
                            .exec("\"" + TEMP_EJECT_PROGRAM.toString() + "\" " + driveLetter + " -L");
                    p.waitFor();
                    Scanner s = new Scanner(p.getInputStream());
                    while (s.hasNextLine())
                        System.out.println(s.nextLine());
                    s.close();
                    System.out.println("Removed " + driveLetter + ".");
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

to remove the drive. The pieces of code above are definately not suited for all aplications and the second one in perticular is not the greatest, there are other much better ways to do it than spawning an anonymus thread... Still however you get the idea behind it :)

Lastly, I sugest you inform the user appropriately and ask for their concent before executing any third-party software in their machine...

I hope this was helpful :-)

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