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 ではサイズが設定されますが、が 未満のsetSize(int size)場合、データが失われる可能性があります。sizefile.getSize()

一部の API では、サイズを 0 に設定することでファイルを切り捨てることができます。これは、内容が削除されることを意味します。実際にサイズ プロパティを設定するのではなく、データを削除してからサイズ プロパティを更新します。

このような混乱を避けるために、この API はsetSize(int size)メソッドを削除しました。

ただし、参照を使用してファイル オブジェクトに内容を追加または削除することで、サイズを間接的に設定することもできますoutputStream

于 2016-11-21T05:53:56.323 に答える
0

パブリック API を使用すると、サイズを設定したり、ファイルを切り捨てたりすることはできません。ただし、特殊なタイプの中には、使用できるメソッドを提供するものもあります。とはいえ、メソッドは公開されていない可能性があり、厄介な反省が必要です。

于 2012-05-20T11:07:10.170 に答える