Think of a file as a one-dimensional array of bytes. You cannot simply delete something from the beginning. Instead, you'll need to fill up the gap by copying over the data. In addition, a file itself is completely unstructured. It doesn't “know” it has lines of text. It is purely convention the after each \n
character, we say a new line begins.
If you want to convert this file:
the first line
the second line
the third line
the fourth line
the fifth line
which actually looks like
{'t', 'h', 'e', ' ', 'f', 'i', 'r', 's', 't', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 's', 'e', 'c', 'o', 'n', 'd', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 't', 'h', 'i', 'r', 'd', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 'f', 'o', 'u', 'r', 't', 'h', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 'f', 'i', 'f', 't', 'h', ' ', 'l', 'i', 'n', 'e', '\n'}
into that file:
the third line
the fourth line
the fifth line
which actually looks like
{'t', 'h', 'e', ' ', 't', 'h', 'i', 'r', 'd', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 'f', 'o', 'u', 'r', 't', 'h', ' ', 'l', 'i', 'n', 'e', '\n', 't', 'h', 'e', ' ', 'f', 'i', 'f', 't', 'h', ' ', 'l', 'i', 'n', 'e', '\n'}
You'll need to figure out up to which byte (in this case the 31st) to skip and then shift all remaining bytes by that amount toward the beginning. This can be done efficiently in Java using memory mapped files (see FileChannel
) but it is not a trivial task.
Instead, you should probably process the file as a stream and write it to another file. Then, eventually, you can rename the new file to overwrite the old one. If you have enough disk space (And you have it, don't you?), this is the easiest way to go.
- Open the input file and create a
BufferedReader
for it.
- Open the output file.
- Repeat for the first n lines in the input:
- Repeat for all remaining lines in the input:
- Write the line to the output file.
- Close both files.