1
私はファイルにbyteArrayを挿入するメソッドを実装しようとしていて、それが意味をなさない場合に挿入されたbyteArrayを含む新しいbyteArrayを設定します。以下は私のコードですが、私のテストでは何の助けも得られないので何か間違っていると確信しています。byteArrayをファイルに書き込む方法は?
public void writeBlock(int blockNum, AbstractDBFile f, AbstractBlock b)
throws IOException {
f.setCurBlockPos(blockNum);
byte[] writeData = new byte[4096];
int currentByte = f.getCurBlockPos() * 4096;
File ourFile = new File(f.getFileName());
// Block block = new Block();
Path path = Paths.get(f.getFileName());
Files.write(path, writeData, StandardOpenOption.WRITE); //Data is Written
RandomAccessFile file = new RandomAccessFile(ourFile, "rw");
FileChannel inChannel = file.getChannel();
ByteBuffer bb = ByteBuffer.allocate(currentByte);
inChannel.write(bb);
while(inChannel.read(bb)>0){
bb.flip();
for (int i =0; i<bb.limit(); i++){
writeData[i]=bb.get();
b.setData(writeData);
}
bb.clear();
}
inChannel.close();
file.close();
}