drive type

R

Roedy Green

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
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top