1

So i'm trying to implement a method that inserts a byteArray into a file, and set a new byteArray containing the byteArray that was inserted if that makes any sense. The following is my code however I'm pretty sure something's wrong since my tests are failing any help would be appreciated.

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();


}
Omar Fayez
  • 103
  • 1
  • 8

1 Answers1

0

I recommend to use the APACHE-COMMON-IO, It's a good lib to do manipulation in files. This lib there are a lots of methods to write, copy, move, etc, with files. It's easy to use.

I had a lots of problems following this way that you made. But nowadays e just use this lib.

Good loock