【发布时间】:2015-01-02 17:46:28
【问题描述】:
我正在创建一个 fileChannel 来执行内存映射写入。此 fileChannel 的大小为 100 字节。我只写了 80 个字节。因此,当我稍后从该文件中读取时,它会在 and 中添加 5 个“0”。有没有办法设置大小或获取写入文件的大小?
非常感谢!!
public FileChannel create(String randomFile){
// create file output stream
RandomAccessFile raf;
FileChannel fc = null;
try {
raf = new RandomAccessFile(randomFile, "rw");
fc = raf.getChannel();
System.out.println("fc.size() "+ fc.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fc;
}
【问题讨论】:
-
你为什么不直接使用
FileChannel.open()? -
我使用 FileChannel 创建一个 MappedByteBuffer.
mbb = fc.map(MapMode.READ_WRITE, position, bufferSize);然后写入该缓冲区
标签: java filechannel