8

I'm writing bytes to a random access file. After completing the operation, I want to delete the first 100 bytes from the file. How can I achieve this?

Thanks in advance.

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
sirin
  • 167
  • 4
  • 9

3 Answers3

5

i found a method which provides the desired works. it's deleteRAF and here.

thanks your advices.

sirin
  • 167
  • 4
  • 9
4

AFAIK, you will need to copy the remaining bytes (file length - 100) to a new file. Deleting the first 100 bytes from a file is not possible without copying the remaining bytes to a new file.

Edit: As cdhowie rightly pointed out, you could:

  • seek to 100,
  • read X amount of bytes (more than 100 though)
  • seek to 0,
  • write X amount of bytes

Then repeat the process until the entire file is written. Finish off by setting the filelength 100 bytes less than previously. If you want to be on the safe side and not risk corrupting the original file, it could be worth writing to a temp file first.

BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37
  • 1
    actually i want rewrite the file. firstly fixed the file size, after reaching the size i'll delete first bytes and append new content to the end of file. – sirin Dec 02 '10 at 14:27
  • Ah sorry, I misunderstood your question then. Just read from a byte offset of 100 bytes, and write the filelength - 100 bytes to a new file. – BlueVoodoo Dec 02 '10 at 14:31
  • No need to use a new file. You can read/seek/write/seek back and forth into the same file. – cdhowie Dec 02 '10 at 14:40
  • @cdhowie: Yes you can but you can't delete the first 100 bytes from a file without copying the bytes into a new file first. Deleting the last 100 bytes from a file is however not a problem. – BlueVoodoo Dec 02 '10 at 14:42
  • but i can't use the second file. i must use only one file. – sirin Dec 02 '10 at 14:47
  • I had a similar issue to yours a half a year ago. There are no smooth work arounds for this one I'm afraid. I would have: 1) copy all bytes - 100 from your file to a temp file using a byte offset 100. Delete the original file. Rename the temp file to what the original is called. Hopefully your files aren't huge and this operation is rather quick. – BlueVoodoo Dec 02 '10 at 14:57
  • @BlueVoodoo: Sorry, that's completely wrong. You can seek within the same file stream. Seek to the 100-byte position. Read 1024 bytes. Seek to the 0-byte position. Write those 1024 bytes. Seek to the 1124 byte position. Read 1024 bytes. Seek to the 1024 byte position. Write those 1024 bytes. See? – cdhowie Dec 02 '10 at 15:49
  • @cdhowie: Yes, you are right, sorry. You can write out the bytes in the same file using your method. I would however still copy to a new file and not risk corrupting the original one but you are right, the method could be done within a the same file. I just updated the answer. – BlueVoodoo Dec 02 '10 at 15:56
  • @BlueVoodoo: This algorithm (if implemented correctly) will never corrupt the initial file, assuming of course that the machine doesn't lose power or the Java program doesn't crash in another thread or something catastrophic like that. – cdhowie Dec 02 '10 at 16:02
  • @cdhowie: Sure. I sometimes operate on very large media files. If the power, OS, app or whatever _DO_ crash, weeks of work could be corrupted in one go. The odds are of course very small but depending on what this app is designed to do, sirin could be better off safe than sorry. – BlueVoodoo Dec 02 '10 at 16:09
  • @BlueVoodoo: Of course. Risk management on their will have to be part of the decision as to which course to pursue. But if the size of the file to be generated is larger than half of the free disk space, there's really not much of a choice. ;) – cdhowie Dec 02 '10 at 16:42
  • @BlueVoodoo How do we delete bytes from the End of a file? Is there a way to set the length? – Pacerier Jan 22 '12 at 04:32
  • @Pacerier - Just set the length of the file to whatever you want it to be. Not too familiar with Java but look the setLength() function of your file handle constructor. – BlueVoodoo Jan 22 '12 at 19:33
  • @BlueVoodoo could you take a look at this: http://stackoverflow.com/q/8959002/632951. thx ! – Pacerier Jan 22 '12 at 22:26
  • @Pacerier - Yes, that looks like the one you are after. In .Net, the equivalent is FileStream.SetLength(). – BlueVoodoo Jan 23 '12 at 09:51
0

There is no simple way to do this. You will have to read starting from byte 100 and write starting from byte 0 -- essentially, shift the file contents down by 100 bytes manually. Then you can truncate the file to 100 bytes less than its length.

cdhowie
  • 158,093
  • 24
  • 286
  • 300