3

I'm running into this nasty problem with Java on Windows. (Java on Linux does not have this problem.)

On Linux, as a root user, I can use new RandomAccessFile(new File("/dev/sdb"), "rw"); to both read AND write raw sectors of my 2nd drive.

On Windows, as an Administrator, I can use RandomAccessFile(new File("//./PhysicalDrive1"), "r"); to read raw sectors of an identical 2nd drive. However, if I specify the "rw" mode instead, I get a java.io.FileNotFoundException.

I can use JNI+C as a workaround on Windows but I'd like to have a non-DLL based cross-platform solution, based solely on Java.

Does anyone have any tips on how to solve this problem?

EDIT:

The exact exception that I get is about some 'parameter' being 'incorrect':

Exception in thread "main" java.io.FileNotFoundException: \\.\PhysicalDrive1 (The parameter is incorrect)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:241)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:122)
    at MyTest.main(MyTest.java:100)
jdphenix
  • 15,022
  • 3
  • 41
  • 74
Harry
  • 3,684
  • 6
  • 39
  • 48
  • possible duplicate of [How to WriteFile to a PhysicalDrive (Windows 7) without getting ERROR\_ACCESS\_DENIED?](http://stackoverflow.com/questions/6608466/how-to-writefile-to-a-physicaldrive-windows-7-without-getting-error-access-den) – Stephen C Nov 24 '13 at 06:32
  • 1
    No, it's NOT a duplicate. Reason: I can get a C program to read and write just fine on both Windows and Linux. I can get a Java program to just read on both Windows and Linux. **However**, I cannot get a Java program to both read *and write** on Windows. – Harry Nov 24 '13 at 06:35
  • No, it's not weird! I've tried even `"\\\\.\\PhysicalDrive1"` to no avail. The forward slashes work fine with the `"r"` mode, anyway. – Harry Nov 24 '13 at 06:36
  • 1
    @StephenC Yes, I have read the question and its answers. Apparently, you've not read carefully what I'm saying above and probably also voted the question for closing. Please don't do that without carefully understanding the problem. I'm having a problem SPECIFICALLY with Java AND on Windows AND with write access... with everything else IDENTICAL between my C and Java programs. – Harry Nov 24 '13 at 06:42
  • Please remove the close vote if it's you behind it. Let more knowledgeable folks out there take a stab at it. – Harry Nov 24 '13 at 06:44
  • If you rewrite your question to make it clearer, and add supporting evidence (e.g. an SSCCE ... in C and Java), then I will consider removing my vote to close. But I won't remove it if you continue to be rude. In the mean time, I still think this is a duplicate ... and that there are other reasons to close as well. – Stephen C Nov 24 '13 at 07:04
  • 1
    Additionally, the target question doesn't have a satisfactory answer; "turn it off and on again" doesn't address whatever issue is causing the problem. – chrylis -cautiouslyoptimistic- Nov 24 '13 at 07:22
  • @StephenC I think it is YOU who's being rude and insensitive. And snobbish: If your bosses are seeing you ask me for my SSCCE evidence just to ask what I believe is a genuine and unique question with no direct or satisfactory answer to date, then they should yank you right now from this role of moderator or whatever that you are here. I see that you've got a 200+K 'reputation' Shame on you and your reputation, really! – Harry Nov 24 '13 at 07:35
  • Well Harry, he doesn't work for Stack Overflow, so his bosses can't do much about what he is doing here. Just a thought though, snapping back probably isn't one of the best ways to defend yourself from accusations of rudeness... also, with as much reputation as he has, neither is he a moderator. I am. Not that it matters to you. – BoltClock Nov 24 '13 at 08:34
  • I agree with you on the "snapping back" part; just wasted my time and energy with him. Appreciate the note. – Harry Nov 24 '13 at 08:43

1 Answers1

0

As @daniel-alder mentioned here, you can try running the following code:

String pathname;
// Full drive:
// pathname = "\\\\.\\PhysicalDrive0";
// A partition (also works if windows doesn't recognize it):
pathname = "\\\\.\\GLOBALROOT\\ArcName\\multi(0)disk(0)rdisk(0)partition(5)";

Path diskRoot = ( new File( pathname ) ).toPath();

FileChannel fc = FileChannel.open( diskRoot, StandardOpenOption.READ,
    StandardOpenOption.WRITE );

ByteBuffer bb = ByteBuffer.allocate( 4096 );

fc.position( 4096 );
fc.read( bb );
fc.position( 4096 );
fc.write( bb );

fc.close();
Community
  • 1
  • 1
wintermute
  • 488
  • 1
  • 9
  • 22