tags:

views:

537

answers:

6

I'm trying to use the following code to access one byte with offset of 50 bytes in a raw disk.

randomAccessFile = new RandomAccessFile("C:", "r");
randomAccessFile.seek(50);
byte[] buffer = new byte[1];
randomAccessFile.read(buffer);

But all what I get is the following error:

java.io.FileNotFoundException: C: (Acceso denegado)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:98)
at pru.lseek.main(lseek.java:26)

Is there any way to access a precise byte in a drive from java?

+1  A: 

As far as I know can't access raw data on a disk from Java

Upul
A: 

Under Linux you can try to open /dev/<device>, e.g. /dev/hda, or /dev/sdb2. This will give you access to a raw disk (or a partition only) but requires that you have appropriate rights—a “normal” user does not have them, though.

Bombe
That is what I'm trying to emulate.f=open("/dev/sda",O_RDONLY);
+1  A: 

In unix, you may read/write from /dev files. (I'm not sure)

In Windows, I think you need to read/write disk sectors via JNI(Java Native Interface). Calls some C library to talk to the OS.

update: In C library, you may need to use Win32API to get the file handle for example CreateFile(..) function.

http://search.cpan.org/~chorny/Win32API-File-0.1101/File.pm

http://jan.newmarch.name/ssw/files/win32.html

teerapap
Well, assuming I will create a JNI library, I don't know what is the first parameter to pass in the 'open' C function: f=open("C:",O_RDONLY); ???
+2  A: 

RandomAccessFile is not meant to open directories to manipulate entries, you need to create or remove files. "Acceso denegado" probably mean access denied. To do this anyway you need JNI.

EDIT: What you are trying to do, is really complicated, there is no common way to do that. You can access the harddisc sector by sector, but then you would have to interpret it's structure, which obviously depends on the file system, FAT,NTFS,HPFS etc.

stacker
trying through JNI, but no luck: fp = fopen("c:", "r")) <- gives no error fseek(fp, i, SEEK_SET) <- gives no error getc(fp) <- always return the same data whatever the i parameter for fseek is
Look here how to read sectors on disk: http://www.codeguru.com/cpp/w-p/system/misc/article.php/c5765/
stacker
Thanks a lot!!that solves my problem:fp = fopen("\\\\.\\C:", "r"))Now I only have to create a JNI library, but it's curios that nobody has created it yet.
+1  A: 

Java can only access files. Unix has the concept of "raw devices" as files in the /dev directory, so what you want is possible there. But not on windows, because it has no such file representation of the raw HD data.

Michael Borgwardt
+1  A: 

In windows you need to access the raw device identifier as a file. It should work if you pass in the file "\\.\c:", you are using the device UNC name \.\c: (\. means this machine).

For Vista and later I don't think it will work correctly as there are mechanisms in place to prevent raw access to the disk for anything other than device drivers (don't quote me on that)

Petesh