I have to read from file what have been overwritten. For example there is file on the disc and size is 75 bytes, then someone add few lines of text and size is 200 bytes. I need to read only this new 125 bytes. I created method for this using RandomAccessFile and it is working fine. I return byte[] bytes then I convert this bytes to String. But what when someone will add 1 GB of text? Is there any way to not read all this bytes to memory, just return new added text?
This is my method
private byte[] readFromFile(String filePath, int position, int size) throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
}
or any better way to do this. I have only size of file in bytes before modified and using this size I have to get new content after modified.