How to determinate if 2 logical drives are on the same physical disc in Java
Asked Answered
I

1

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

Intestate answered 18/3, 2019 at 20:23 Comment(5)
I assume answers should be windows only, as you used windows hard disk names?Moody
Yeah, it's WIndows-onlyIntestate
Have you tried accessing the registry in similar fashion to this solution at the second-to-bottom of https://mcmap.net/q/223994/-how-to-list-physical-disks ?Featurelength
If you are running your programm on a system where you got powershell this could be a way to go. In addition with executing-powershell-commands-in-java.Evangelinaevangeline
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?Intestate
C
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
Celebrity answered 18/3, 2019 at 22:54 Comment(3)
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 drivesIntestate
It would seem that getAttribute(String attribute) is what we need to use. I am however unable to find documentation on FileStore attributes.Celebrity
getAttribute support values: totalSpace, usableSpace, unallocatedSpace, volume:vsn, volume:isRemovable and volume:isCdrom for Concrete class WindowsFileStoreIntestate

© 2022 - 2024 — McMap. All rights reserved.