Link to home
Start Free TrialLog in
Avatar of mag062397
mag062397

asked on

Making a RamdomAccessFile shorter.

How do you make a file shorter in java?
Say I want to open a RamdomAccessFile and delete the last 100 bytes, how do I just tell the system that the EOF should come 100 bytes before the end.

The seek method lets me seek past the end of the file and write something; thereby, making the file larger.  But, I want to make it smaller without having to copy the entire file.

Is there a way to do this?
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S. if you can indicate why this is important, perhaps an alternative can be concocted.
Avatar of mag062397
mag062397

ASKER

What do you mean by "..., nor operating systems in general, provide this." ?

I can see that Java doesn't provide this, but surely it can be do in C, right?  I mean surely C can modify a directory entry, right?


Theoretically. However, operating systems in general don't facilitate such an operation. In DOS, although it might be tractable to find the directory entry and change the size parameter in it, you would now also be responsible for fixing up the FAT table to compensate for any freed clusters. In Unix, you would have to have root priviledges.
You CAN do it in C. It is a language, after all, that was first used to write an operating system in. But to do it, you wind up doing exactly that: writing operating system level code. Not pretty; not portable; not recommended.
How do you think editor's do it?  Do you think they make a new file and delete the old?
Yup. In general, what an editor does, is read in the file when you start editing it. You then make some large group of (from its point of view) random changes to the file. There is no reasonable way for it to apply these changes to the file on disk. When you save your work, it simply deletes the existing file, and writes a new one.

Okay.  Thanks.