RandomAccessFile raf = null;
try {
raf = new RandomAccessFile("\\\\.\\PhysicalDrive0","r");
byte [] block = new byte [2048];
raf.seek(0);
raf.readFully(block);
System.out.println("READ BYTES RAW:\n" + new String(block));
} catch (IOException ioe) {
System.out.println("File not found or access denied. Cause: " + ioe.getMessage());
return;
} finally {
try {
if (raf != null) raf.close();
System.out.println("Exiting...");
} catch (IOException ioe) {
System.out.println("That was bad.");
}
}
但是如果我切换到“rw”模式,会出现NullPointerException,即使我以管理员身份运行程序,我也没有获得原始写入磁盘的句柄。我知道这已经被问到了,但主要是为了阅读......那么,写作呢?我需要JNI吗?如果有,有什么建议吗?
干杯
答案 0 :(得分:2)
您的问题是new RandomAccessFile(drivepath, "rw")
使用与原始设备不兼容的标记。要写入此类设备,您需要Java 7及其新的nio类:
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();
(答案来自另一个(类似的)question)