3 Replies Latest reply on Jul 23, 2010 7:26 PM by 843790

    How to detect which physical drive a given file is on

    843790
      Given a file that exists, is there a way within Java to detect what physical drive that file is on? I don't care how that drive is denoted, as long as it is uniquely specified. This can be through a hard drive serial number, or any other way. The specifics of the drive do not matter, I only care about uniquely identifying physical drives. If it matters, I'd like to also be able to determine if said drive is a flash drive, however programmatically I won't care that it is a flash drive, just that it is a separate drive itself.

      If this isn't possible through Java, could someone point me in the direction of how to determine this through another language, possibly using JNI?
        • 1. Re: How to detect which physical drive a given file is on
          EJP
          It depends entirely on your operating system, so at best you would be writing OS-specific JNI code.

          However on some configurations the question doesn't even have any meaning. I was using file systems striped across multiple disks as long ago as the early 1990s. The same would apply to any RAID array now.
          • 2. Re: How to detect which physical drive a given file is on
            843790
            mgolowka wrote:
            Given a file that exists, is there a way within Java to detect what physical drive that file is on?
            Check if this helps you, I'm not sure the effects when this is executed in a Unix based environment.
                 public static void main(final String[] args) {
                      final String fileName = "my_file.myext";
                      final File file = new File(fileName);
                      final File roots[] = File.listRoots();
                      final String rootOfTheFile = file.getAbsolutePath().substring(0,
                                file.getAbsolutePath().indexOf(File.separator))
                                + File.separator;
            
                      System.out.println("File is present in - " + rootOfTheFile);
                      for (final File file2 : roots) {
                           System.out.println("Root in system - " + file2.getPath());
                      }
                 }
            And the output for the code is as follows, of which Y and Z are network mapped drives.
            File is present in - D:\
            Root in system - C:\
            Root in system - D:\
            Root in system - E:\
            Root in system - F:\
            Root in system - Y:\
            Root in system - Z:\
            • 3. Re: How to detect which physical drive a given file is on
              843790
              Unfortunately this won't work in a unix environment, and it doesn't totally work in a windows environment either because C: and D: (or any other drives) can just be partitions on the same drive. C: doesn't tell me for sure that it's a physical drive.

              Regarding a RAID setup, I do realize that a RAID would complicate this problem, however for my purposes right now, I can ignore that problem.