How to access specific raw data on disk from java

I was looking by myself for a possibility to access raw data form a physical drive. And now as I got it to work, I just want to tell you how. You can access raw disk data directly from within java … just run the following code with administrator priviliges:

    File diskRoot = new File ("\\\\.\\PhysicalDrive0");
    RandomAccessFile diskAccess = new RandomAccessFile (diskRoot, "r");
    byte[] content = new byte[1024];
    diskAccess.readFully (content);

So you will get the first kB of your first physical drive on the system. To access logical drives – as mentioned above – just replace ‘PhysicalDrive0’ with the drive letter e.g. ‘D:’

oh yes … I tried with Java 1.7 on a Win 7 system …

Just have a look at the naming of physical drives at http://support.microsoft.com/kb/100027/en-us

Leave a Comment