[英]How to get raw disk access in Java with write permissions - Windows 7
好的,相信我,這是我想要這樣做的原因。 也許不使用Java,但是有。 我可以使用UNC樣式的路徑來原始訪問Windows 7上的磁盤,例如:
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嗎? 如果是這樣,有什么建議嗎?
干杯
您的問題是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();
(答案來自另一個(類似) 問題 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.