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?
mag062397Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
No. Filesystems in general have adopted the worldview that Unix pioneered namely: a file is a stream of bytes. One consequence of this is that there is no "EOF" designator at the end of the file. The size of the file is described in the directory entry for the file. Consequently the only way to make a file shorter without copying it, is to change the directory entry, and return any now unused chunks of space to the free list. Java does not, nor operating systems in general, provide this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imladrisCommented:
P.S. if you can indicate why this is important, perhaps an alternative can be concocted.
0
mag062397Author Commented:
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?


0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

imladrisCommented:
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.
0
mag062397Author Commented:
How do you think editor's do it?  Do you think they make a new file and delete the old?
0
imladrisCommented:
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.

0
mag062397Author Commented:
Okay.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.