mP. mP. - 19 days ago 7
Java Question

Apache Commons VFS - How does one set the size of a file...?

I can't seem to find any method on FileObject or FileContent which allows me to set a new size on an existing file.

FileContent.getSize()
does not have a corresponding
setSize()
.
RandomAccessContent
does have some comments about seeking but it does not appear to state that a file will shrink if one seeks somewhere before the end and then simply closes the file.

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

mP. mP.
Answer

using the public APIs you cant set the size or truncate any file. However some specialised types do offer a method that may be used. That said the method may also be not public requiring nasty reflection.