Uncover partition

Asked

Viewed 99 times

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.

1 answer

1


I thought of a "gambiarra" here :p. You could take the full path in which the application is using the method getCanonicalPath of java.io.File, and then filter the char of the partition using the method charAt of java.lang.String. I’ve designed this little example for you to test. Theoretically, it should print the partition on which the application is running.

import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        String applicationPath = new File(".").getCanonicalPath();
        char partition = applicationPath.charAt(0);
        System.out.println(partition);
    }
}

If you want the partition the operating system is in, just filter through the home folder path (Cross-platform):

char osPartition = System.getProperty("user.home").charAt(0);
  • I think he wants the way of the operating system and not the application.

  • I edited the question including how to find the OS partition, @Guilhermenascimento

  • 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.

  • 1

    @Marcel then Ericson’s answer seems to answer

  • @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.

  • @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, 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?

  • @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.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.