4

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;
skaffman
  • 398,947
  • 96
  • 818
  • 769
mP.
  • 18,002
  • 10
  • 71
  • 105
  • 1
    I maybe missing the point of your question, but the only way to change the size of a file is to change the contents. Simply changing the seek point does not make any changes to the file. Are you wanting to truncate a file at some point? – vickirk Jul 18 '11 at 10:19
  • Not quite many apis allow one for example to clear a file by simply setting its size to 0 rather than recreating it. Setting a file size may also be used to perhaps keep only the start and "delete" the remainder of a file without creating another file, copying, deleting the original and renaming it back etc. – mP. Jul 18 '11 at 12:43
  • other apis yes, but not this one which is what your question refers to. Still n9ot sure what you want, try being clearer and someone may answer. if you want to truncate local file try the truncate operation on the filechannel via the `getLocalFile`, whether this is useful to you I have no idea – vickirk Jul 18 '11 at 14:41
  • @vickirk, i was hoping to avoid the need to cast, because that would mean i would need to special case for each different vfs filesystem, which i was hoping to do by using the generic API. I guess i might need to do something like what i think your pointing towards, but fingers crossed. – mP. Jul 19 '11 at 01:43
  • In answer to your original comment, yes i want to truncate at some point!.. sorry for not beng clear about this... – mP. Jul 19 '11 at 01:44

2 Answers2

0

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.

mP.
  • 18,002
  • 10
  • 71
  • 105
0

If you want to set the size by using setSize(int size) it would set the size in some APIs but there might be some data loss when size is less than file.getSize().

Some APIs will still allow to truncate the file by setting the size to 0, which means that the contents are removed. It is not actually setting the size property rather it's removing the data and then refreshing the size property.

To avoid such confusion, this API has removed the setSize(int size) method.

But you can still set the size indirectly by appending or removing contents to or from the file object by using an outputStream reference.

Aryasindhu Sahu
  • 103
  • 1
  • 13