繁体   English   中英

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

[英]Apache Commons VFS - How does one set the size of a file…?

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

FileContent.getSize()没有对应的setSize() RandomAccessContent确实有一些关于搜索的评论,但在 state 看来,如果在结束前搜索某个位置然后简单地关闭文件,文件将缩小。

/**
 * 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;

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

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

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

为了避免这种混淆,这个 API 删除了setSize(int size)方法。

但是您仍然可以通过使用outputStream引用在文件 object 中附加或删除内容来间接设置大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM