drive type

Discussion in 'Java' started by Roedy Green, Dec 31, 2012.

  1. Roedy Green

    Roedy Green Guest

    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
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Students who hire or con others to do their homework are as foolish
    as couch potatoes who hire others to go to the gym for them.
    Roedy Green, Dec 31, 2012
    #1
    1. Advertising

  2. Roedy Green

    Arne Vajhøj Guest

    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
    Arne Vajhøj, Dec 31, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon
    Replies:
    5
    Views:
    11,165
    Scott M.
    Jan 17, 2010
  2. mh
    Replies:
    4
    Views:
    701
    Wolfgang Strobl
    May 31, 2005
  3. Tom Anderson
    Replies:
    8
    Views:
    394
    Gerrit Holl
    Sep 15, 2005
  4. Mike Joyce
    Replies:
    3
    Views:
    3,139
    Gordon Rainsford
    May 3, 2006
  5. shailesh
    Replies:
    1
    Views:
    783
    Tim Golden
    Mar 28, 2007
Loading...

Share This Page