首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache Commons VFS -如何设置文件的大小...?

Apache Commons VFS -如何设置文件的大小...?
EN

Stack Overflow用户
提问于 2011-07-18 18:13:52
回答 2查看 828关注 0票数 4

我似乎在FileObject或FileContent上找不到任何允许我在现有文件上设置新大小的方法。

FileContent.getSize()没有对应的setSize()RandomAccessContent确实有一些关于查找的评论,但它似乎没有声明,如果一个人在结束之前的某个地方查找,然后简单地关闭文件,那么文件将会缩小。

代码语言:javascript
复制
/**
 * Sets the file-pointer offset, measured from the beginning of this
 * file, at which the next read or write occurs.  The offset may be
 * set beyond the end of the file. Setting the offset beyond the end
 * of the file does not change the file length.  The file length will
 * change only by writing after the offset has been set beyond the end
 * of the file.
 * <br/>
 * <b>Notice: If you use {@link #getInputStream()} you have to reget the InputStream after calling {@link #seek(long)}</b>
 *
 * @param pos the offset position, measured in bytes from the
 *            beginning of the file, at which to set the file
 *            pointer.
 * @throws IOException if <code>pos</code> is less than
 *                     <code>0</code> or if an I/O error occurs.
 */
public void seek(long pos) throws IOException;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-20 19:07:10

使用公共API,您不能设置大小或截断任何文件。然而,一些专门化的类型确实提供了一种可以使用的方法。也就是说,该方法也可能不是公开的,需要进行令人讨厌的反思。

票数 0
EN

Stack Overflow用户

发布于 2016-11-21 13:53:56

如果您想使用setSize(int size)设置大小,它会在某些API中设置大小,但是当size小于file.getSize()时,可能会有一些数据丢失。

一些API仍然允许通过将大小设置为0来截断文件,这意味着内容将被删除。它实际上并没有设置size属性,而是删除数据,然后刷新size属性。

为了避免这种混淆,本接口去掉了setSize(int size)方法。

但是,您仍然可以通过使用outputStream引用将内容附加到文件对象或从文件对象中删除内容来间接设置大小。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6731392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档