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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With