java 如何从java访问磁盘上的特定原始数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access specific raw data on disk from java
提问by user178973
I'm trying to use the following code to access one byte with offset of 50 bytes in a raw disk.
我正在尝试使用以下代码访问原始磁盘中偏移量为 50 字节的一个字节。
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?
有没有办法从java访问驱动器中的精确字节?
采纳答案by stacker
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.
RandomAccessFile 并不是为了打开目录来操作条目,您需要创建或删除文件。“Acceso denegado”可能意味着访问被拒绝。无论如何,要做到这一点,您需要 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.
编辑:你想要做的事情真的很复杂,没有通用的方法来做到这一点。您可以逐个扇区访问硬盘,但是您必须解释它的结构,这显然取决于文件系统、FAT、NTFS、HPFS 等。
回答by hunsricker
I was looking by myself for a possibility to access raw data form a physical drive. And now as I got it to work, I just want to tell you how. You can access raw disk data directly from within java ... just run the following code with administrator priviliges:
我一直在寻找从物理驱动器访问原始数据的可能性。现在当我让它工作时,我只想告诉你如何。您可以直接从 java 中访问原始磁盘数据……只需以管理员权限运行以下代码:
File diskRoot = new File ("\\.\PhysicalDrive0");
RandomAccessFile diskAccess = new RandomAccessFile (diskRoot, "r");
byte[] content = new byte[1024];
diskAccess.readFully (content);
So you will get the first kB of your first physical drive on the system. To access logical drives - as mentioned above - just replace 'PhysicalDrive0' with the drive letter e.g. 'D:'
因此,您将获得系统上第一个物理驱动器的第一个 kB。要访问逻辑驱动器 - 如上所述 - 只需将“PhysicalDrive0”替换为驱动器号,例如“D:”
oh yes ... I tried with Java 1.7 on a Win 7 system ...
哦,是的......我在Win 7系统上尝试过Java 1.7......
Just have a look at the naming of physical drives at http://support.microsoft.com/kb/100027/en-us
只需在http://support.microsoft.com/kb/100027/en-us 上查看物理驱动器的命名
回答by Daniel Alder
If you are interested in writingto a raw volume under Windows, try this (needs Java 7).
如果您有兴趣在Windows 下写入原始卷,请尝试此操作(需要 Java 7)。
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();
Of course, you have to make sure the device is writable and not accessed/locked by the system. Also make sure your application runs with the necessary privileges (elevated privileges).
当然,您必须确保设备可写且不被系统访问/锁定。还要确保您的应用程序以必要的权限(提升的权限)运行。
Btw: Using new RandomAccessFile(drive, "rw")doesn't seem to work because Java doesn't open the file handle in a mode which is compatible to raw devices (exception is java.io.FileNotFoundException (The parameter is incorrect)). But reading works fine also with RandomAccessFile.
顺便说一句:使用new RandomAccessFile(drive, "rw")似乎不起作用,因为 Java 不会以与原始设备兼容的模式打开文件句柄(例外是java.io.FileNotFoundException (The parameter is incorrect))。但阅读也适用于RandomAccessFile.
回答by Bombe
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.
在 Linux 下,您可以尝试打开/dev/<device>,例如/dev/hda, 或/dev/sdb2。这将使您能够访问原始磁盘(或仅分区),但需要您具有适当的权限——不过,“普通”用户没有这些权限。
回答by teerapap
In unix, you may read/write from /devfiles. (I'm not sure)
在 unix 中,您可以读取/写入/dev文件。(我不确定)
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.
在 Windows 中,我认为您需要通过 JNI(Java 本机接口)读取/写入磁盘扇区。调用一些 C 库与操作系统对话。
update: In C library, you may need to use Win32API to get the file handle for example CreateFile(..)function.
更新:在 C 库中,您可能需要使用 Win32API 来获取示例CreateFile(..)函数的文件句柄。
https://metacpan.org/pod/Win32API::File
https://metacpan.org/pod/Win32API::File
回答by Michael Borgwardt
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.
Java 只能访问文件。Unix 将“原始设备”的概念作为 /dev 目录中的文件,所以你想要的在那里是可能的。但不是在 Windows 上,因为它没有原始高清数据的这种文件表示。
回答by Petesh
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).
在 Windows 中,您需要将原始设备标识符作为文件访问。如果您传入文件“\\.\c:”,它应该可以工作,您使用的是设备 UNC 名称 \.\c:(\. 表示这台机器)。
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)
对于 Vista 及更高版本,我认为它不会正常工作,因为有一些机制可以防止设备驱动程序以外的任何内容对磁盘进行原始访问(不要引用我的话)
回答by momaison
@hunsricker : note that accessing raw devices require some privileges (depends on drive : removable or not / depends on file system for WinXP : iso9660 is allowed, FAT is not).
@hunsricker:请注意,访问原始设备需要一些权限(取决于驱动器:是否可移动/取决于 WinXP 的文件系统:允许使用 iso9660,不允许使用 FAT)。
Note also that size of read does matter (depending on OS) : On an iso9660 filesystem, read(1024 bytes) works on XP but fails on Seven. On Seven it look like the reads must be block aligned : read(2048 bytes) works.
另请注意,读取的大小确实很重要(取决于操作系统):在 iso9660 文件系统上,读取(1024 字节)在 XP 上有效,但在 7 上失败。在七上看起来读取必须是块对齐的:读取(2048 字节)有效。

