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