How Do I Eject a Volume in Java?

时间秒杀一切 提交于 2019-11-30 17:50:42

问题


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?


回答1:


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.




回答2:


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 :-)



来源:https://stackoverflow.com/questions/1049053/how-do-i-eject-a-volume-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!