0
I’m creating an application that will run from an external hard drive. I have been researching but I haven’t found a way without using too much gambiarra to find the partition where this hard drive was mounted.
My goal is to have a File object that has as its path the root of the partition on which the hard drive is mounted. For example: if the hard drive is mounted on "E: " (in case the OS is Windows), my File object must have this path.
If anyone knows anything that can help me, please respond.
I think he wants the way of the operating system and not the application.
– Guilherme Nascimento
I edited the question including how to find the OS partition, @Guilhermenascimento
– Ericson Willians
Actually @Guilhermenascimento, I would like to take the path from where the application is running (external HD). When plugging in the HD eventually it will be mounted on different partitions, wanted to get this information at runtime. I’m sorry I wasn’t clear earlier.
– Marcel
@Marcel then Ericson’s answer seems to answer
– Guilherme Nascimento
@Marcel, the getCanonicalPath method I quoted there, returns the path the application is in, regardless of the partition. Here it returns "E", because my Workspace is in E :p There is not much secret, test there to see if it works.
– Ericson Willians
@Ericsonwillians Thanks for the reply. I had thought of something similar, my reluctance to use this was that it is a specific solution for Windows. I figured there was some API or third-party method that offered such a feature (making the application more portable). Anyway thank you for the good will, thank you very much.
– Marcel
@Marcel, in Unix environment, you will also receive the path independent of the partition you are in. The difference is that the partition filter relative to String will not be the same. The partition can be "/dev/hda1" or whatever, you have to use a Regex or manipulate the String otherwise. To make it cross-platform, you need to recognize which operating system the application is running on. You can do this by checking the
System.getProperty("os.name")
. However, it will be boring to test :p Your customers will use different operating systems?– Ericson Willians
@Ericsonwillians, actually the application will be for own use (automated backup of personal files - not necessarily mine). I am Windows user but eventually use Linux on a machine.
– Marcel