Re: drive type
On 12/31/2012 9:33 AM, Roedy Green wrote:
Is there a way inside java to find out what sort of storage a drive or
file is?
e.g.
hard disk
SSD
CD
DVD
USB
remote drive
Supported?
Write some C code and access it via JNI.
Unsupported, may break any time, completely OS and
Java implementation dependent, try something like
this:
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Storagedetails {
public static void dump(String indent, Object o) throws
IllegalArgumentException, IllegalAccessException {
for(Field f : o.getClass().getDeclaredFields()) {
System.out.print(indent + f.getName() + " : " + f.getType().getName());
f.setAccessible(true);
Object o2 = f.get(o);
if(o2.getClass().getPackage().getName().startsWith("java.lang")) {
System.out.println(" = " + o2);
} else {
System.out.println();
dump(indent + " ", o2);
}
}
}
public static void fsdump(String pathstr) throws IOException,
IllegalArgumentException, IllegalAccessException {
dump("", Files.getFileStore(Paths.get(pathstr)));
}
public static void main(String[] args) throws Exception {
fsdump("C:\\");
}
}
On my Windows 7 it outputs:
root : java.lang.String = C:\
volInfo : sun.nio.fs.WindowsNativeDispatcher$VolumeInformation
fileSystemName : java.lang.String = NTFS
volumeName : java.lang.String = ARNEPC4
volumeSerialNumber : int = -126338268
flags : int = 65470719
volType : int = 3
displayName : java.lang.String = ARNEPC4
$assertionsDisabled : boolean = true
Volume types probably match those in:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939.aspx
Go have fun with Linux, IBM Java and all the other combinations.
:-)
Arne