7

Imagine a PC with an SSD, and a HDD.

SSD is splitted to 2 partitions: C and D.

HDD is splitted to 2 partitions: E and F.

I need to create a method:

boolean isOnSamePhysicalDrive(String drive1, String drive2);

isOnSamePhysicalDrive("C", "D") --> true

isOnSamePhysicalDrive("E", "F") --> true

isOnSamePhysicalDrive("C", "E") --> false

Saphyra
  • 450
  • 3
  • 15
  • 1
    I assume answers should be windows only, as you used windows hard disk names? – Ferrybig Mar 18 '19 at 20:25
  • Yeah, it's WIndows-only – Saphyra Mar 18 '19 at 20:28
  • Have you tried accessing the registry in similar fashion to this solution at the second-to-bottom of https://stackoverflow.com/questions/327718/how-to-list-physical-disks ? – Ishmael Mar 18 '19 at 20:35
  • If you are running your programm on a system where you got powershell [this](https://superuser.com/a/1147305) could be a way to go. In addition with [executing-powershell-commands-in-java](https://stackoverflow.com/questions/29545611/executing-powershell-commands-in-java-program/29545926). – FlorianDe Mar 18 '19 at 20:55
  • Closer, but one issue: My Laptop currently has the following setup: CEF drives on SSD, DGH drives on HDD, and I on External HDD. When I run command 'powershell.exe Get-Disk (Get-Partition -DriveLetter 'H').DiskNumber' it prints error for DGH drives. Maybe because HDD is dynamic disc? – Saphyra Mar 18 '19 at 21:15

1 Answers1

3

Java.nio.file.FileStore is what you are looking for.

https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileStore.html

Storage for files. A FileStore represents a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storage.

This code prints the names of my partitions when executed.

for (FileStore fs: FileSystems.getDefault().getFileStores()) {
    System.out.println("Name: " + fs.name());
    System.out.println("Type: " + fs.type());
}

As such

Name: SSD
Type: NTFS
Name: Door systeem gereserveerd
Type: NTFS
Name: 
Type: NTFS

Note that Door systeem gereserveerd is a partition of my main drive, SSD. Excuse the Dutch language.

enter image description here

Lokale schijf means Local drive . The disk is unnamed, which is why no name shows up in the results.

To be more specific, you can use this.

System.out.println(Files.getFileStore(Paths.get("C:/")).name());
System.out.println(Files.getFileStore(Paths.get("E:/")).name());

Will print the name of a specific drive or partition. In my case:

SSD
Door systeem gereserveerd
Kars
  • 845
  • 1
  • 14
  • 33
  • `FileSystems.getDefault().getFileStores().forEach(fileStore -> System.out.println(fileStore.name() + " - " + fileStore.type()));` returns Windows - NTFS HDD Programs - NTFS SSD Prog - NTFS Develop - NTFS Torrent - NTFS Storage - NTFS ext_storage - NTFS For me, however these drives takes place on 3 physical drives (SSD, HDD, External HDD), and I cannot see the mapping of the physical / logical drives – Saphyra Mar 18 '19 at 23:08
  • It would seem that `getAttribute(String attribute)` is what we need to use. I am however unable to find documentation on `FileStore` attributes. – Kars Mar 18 '19 at 23:15
  • getAttribute support values: totalSpace, usableSpace, unallocatedSpace, volume:vsn, volume:isRemovable and volume:isCdrom for Concrete class WindowsFileStore – Saphyra Mar 18 '19 at 23:22