Getting an java.io.IOException: The parameter is incorrect when trying to read/write to a disk partition

15 hours ago 1
ARTICLE AD BOX

I am trying to read/write chunks bytes on a disk partition (lettered W). However, when I try to use a RandomAccessFile to do this, it always tells me that my parameter is incorrect on the line reading or writing anything.

My code:

File f = new File("\\\\.\\W:"); try { RandomAccessFile raf = new RandomAccessFile(f, "rw"); raf.seek(0); byte[] bytes = new byte[1]; raf.read(bytes); raf.seek(raf.length()); raf.write(0x2A); raf.close(); } catch (IOException e) { throw new RuntimeException(e); }

The program will throw the error when running raf.read(bytes) or raf.write(0x2A). I suspect the problem is with the path of the file based on other questions I've browsed, but I have no idea how to remedy it. The weird thing is, I have another method that uses a RandomAccessFile to read the whole disk just like this, and it works just fine using raf.readFully()

The stack trace:

Exception in thread "main" java.lang.RuntimeException: java.io.IOException: The parameter is incorrect at Env.main(Env.java:18) Caused by: java.io.IOException: The parameter is incorrect at java.base/java.io.RandomAccessFile.readBytes0(Native Method) at java.base/java.io.RandomAccessFile.readBytes(RandomAccessFile.java:428) at java.base/java.io.RandomAccessFile.read(RandomAccessFile.java:501) at Env.main(Env.java:12)

What I've tried:

Running the program as an administrator

Changing the file path to a text file on the W drive (Which DOES read/write with no problems)

Using a FileOutputStream, but the same problem occurs

Read Entire Article