Discover partition [closed]

0

I'm creating an application that will run from an external hard drive. I've been researching but I have not found a way without using a lot of gambiarra to find the partition where that HD was mounted.

My goal is to have a File object that has the path to the root of the partition on which the HD is mounted. For example: if the HD is mounted on "E: \" (in case the OS is Windows), my File object should have this path.

If anyone knows anything that can help me, please respond.

    
asked by anonymous 24.11.2016 / 20:48

1 answer

1

I thought of a "gambiarra" here: p. You could pick up the full path where the application is using the getCanonicalPath from java.io.File , and then filter the partition's char using the charAt method. from java.lang.String . I made 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 on which the operating system is located, simply filter by the path of the home folder:

char osPartition = System.getProperty("user.home").charAt(0);
    
24.11.2016 / 21:06