读取磁盘的指定扇区内容,基于Java实现,要求root权限。
/**
* 读取磁盘或TF卡指定扇区
* @param device 设备,如/dev/sda
* @param sector 扇区号
* @param size 扇区大小,字节
* @return 扇区内容
*/
public byte[] readDiskSector(String device, int sector, int size) throws IOException {
byte[] sectorBytes = null;
FileChannel fc = null;
try {
Path fp = Paths.get(device);
fc = FileChannel.open(fp, EnumSet.of(StandardOpenOption.READ));
ByteBuffer buffer = ByteBuffer.allocate(size);
fc.read(buffer, sector * size);
fc.close();
sectorBytes = buffer.array();
}
finally {
if(fc != null)
fc.close();
}
return sectorBytes;
}/**
*