4

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

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

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

2 回答 2

0

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

于 2012-05-20T11:07:10.170 回答
0

如果您想通过使用setSize(int size)它来设置大小,则会在某些 API 中设置大小,但是当size小于file.getSize().

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

为避免此类混淆,此 API 已删除该setSize(int size)方法。

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

于 2016-11-21T05:53:56.323 回答