gpt4 book ai didi

java - 如何将 byteArray 写入文件?

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:17 27 4
gpt4 key购买 nike

因此,我正在尝试实现一种将 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();


}

最佳答案

我推荐使用APACHE-COMMON-IO,这是一个很好的文件操作库。这个库有很多方法可以用文件来写入、复制、移动等。它很容易使用。

按照你的方法我遇到了很多问题。但现在我们只使用这个库。

好看

关于java - 如何将 byteArray 写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com