1

I have a database txt file where I want to manipulate certain lines. For example: I want to remove the Dataset in line 44, or edit the Dataset in line 22. I have tried the FileWriter and the RandomAccessFile, which both seem not fit to do the job. The FileWriter is only able to append text to the file and doesnt use a pointer to navigate through the database. The RandomAccessFile seems good for the job, but I keep getting the problem, that when I used methods of the RAF on my txt file the file encoding breaks and as soon as i open it i can only see strange Unicode symbols. Is it possible to use the RAF Class without breaking the encoding or is there another class to manipulate files at specific byte locations or at specific lines. Thanks in advance!

The code for RAF:

public static void main(String[] args)
{
    try
    {
        RandomAccessFile raf = new RandomAccessFile("test123.txt", "rw");
        raf.seek(0);
        raf.writeChars("First line of text" + System.lineSeparator());
        raf.writeChars("Second line of text");
        raf.close();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Out put in the File is: F i r s t l i n e o f t e x t

S e c o n d l i n e o f t e x t

skonline90
  • 53
  • 7
  • Have a look at this post https://stackoverflow.com/questions/9964892/how-to-read-utf8-encoded-file-using-randomaccessfile you need to specify the encoding when reading lines so they don't get corrupted – Bentaye Feb 15 '18 at 10:42
  • Possible duplicate of [How to read UTF8 encoded file using RandomAccessFile?](https://stackoverflow.com/questions/9964892/how-to-read-utf8-encoded-file-using-randomaccessfile) – Bentaye Feb 15 '18 at 10:49
  • How do you know the encoding anyway? I created the file using File class just using the String "test.txt" – skonline90 Feb 15 '18 at 10:53
  • Then put your code in your question and we can have a look – Bentaye Feb 15 '18 at 10:53
  • You should decide wether you want a text file (then use only Reader and Writer) or a binary file with blocks of bytes, which may be interpreted as strings or records or whatever. Don't try to manipulate a text file with RandomAccessFile. To manipulate a text file you should read it in as text and write the changed version as text. You don't need to hold the whole file in memory but it is always read as text, manipulate, write as text. Don't try to delete a line in a text file with RandomAccessFile (except you know exactly what you do). – vanje Feb 15 '18 at 12:08
  • I am quite open to use the reader and writer. But as i said in the beginning, it doesnt give me the freedom to f.e. move to line 500 and edit the text there. Or am I wrong on this? If not then I see the RAF as the only option for my programm. – skonline90 Feb 15 '18 at 13:45
  • Hm, I think your only misconception is that you think RAF is good for manipulating text files. Sure, you can replace some characters, but only if the new characters are encoded with the exact same number of bytes than the original chars. How would you delete a character in line 500? First you find the start position and the number of bytes for this character. Then you have to move all bytes behind forward to close the gap. Or to insert one character you have to shift back all bytes at insertion position to make room for your new bytes. This is inefficient and error prone. – vanje Feb 16 '18 at 12:56
  • So instead trying to modify a text file in-situ, the recommended way to modify line 500 of a text file is: Read the first 499 and write them as is to a new temporary file. Then read line 500 and modify it. Write line 500 to the temp file. Then read all remaining lines and write them unmodified to the temp file. Then close the temp file. Remove the original file and move the temp file to the original position. If this is still too slow for you (you should do some real performance tests) then you should consider a different file format to perform fast changes and export the text only at the end. – vanje Feb 16 '18 at 13:05

0 Answers0