我正在创建一个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;
}发布于 2015-01-02 17:53:25
用这个代替:
final Path path = Paths.get("thefile");
final FileChannel fc = FileChannel.open(path, StandardOpenOption.WRITE,
StandardOpenOption.READ);
// then later:
fc.truncate(80L);当然,别忘了.close()。理想情况下,您应该使用一个试用资源语句打开您的通道。
发布于 2015-01-02 17:49:32
我认为setLength()是你要找的。爪哇医生。
https://stackoverflow.com/questions/27746292
复制相似问题