Mount and unmount hard drives

醉酒当歌 提交于 2019-12-01 06:43:25

问题


How can I mount and unmount hard drives (platform independent, so no using Runtime to execute a hard-coded command) in the Java Programming Language?


回答1:


The answer is "Yes And No". You could not mount or unmount devices in java because each OS has their own methods to do this. BUT... you can provide java api that use adapter pattern for native interface. You should do some things:

  • create Java interfaces that support mount/unmount commands
  • create classes that implements interfaces as native methods
  • create native implementations of this commands in C or other language. One implemantation for OS (Win, Mac, Linux)
  • pack it to one jar
  • build small factory that provide implementation of interface and load native libraries

It is not pure java, but is good solution I think. Client code will use java.




回答2:


As there is no way to do this portably (or even with the same semantics), there is no built-in Java method to do so.




回答3:


"Platform independent" doesn't mean you are not allowed to use external processes. You can check the OS your app is runnning on. See System.getProperty("os.name"); Depending on the result of that method, invoke the correct process.




回答4:


Think about it: Java is supposed to work on platforms where no hard drives exist, so how could there be a platform independent way of doing this?
Besides, even if you restricted it to platforms where hard disks are available, how is Java supposed to abstract away the different approaches to RAIDs / partitions / slices etc?

Why do you want/need to do this?



来源:https://stackoverflow.com/questions/5505682/mount-and-unmount-hard-drives

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