Introduction
I challenged myself with this proyect.
I need to modify data at a specific line on a .txt without doing the next things:
A simple .txt file will have everything!
Text file
1;asd1324;2019-05-22 18:28:56;0;0;
2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;
follows this format which are Strings with ";" as separator.
My code try which works for numbers from 0 to 9
(0 to 9 means this first number -> 1;asd1324;2019-05-22 18:28:56;0;0;)
public static void test(int lineNumber, String data)
{
String line;
try{
System.out.println("----DEBUG TEST----\n");
RandomAccessFile file = new RandomAccessFile("parking.txt", "rw");
System.out.println("File pointer is: " + file.getFilePointer());
System.out.println("Line size is: " + file.readLine().length());
System.out.println("Read line is: " + (line = file.readLine()) + " with size: " + line.length());
file.seek(line.length());
System.out.println("File pointer is: " + file.getFilePointer());
file.writeChars("\n");
file.writeBytes("7;asd1324;2019-05-22 18:28:56;0;0;");
file.writeChars("\n");
System.out.println("File pointer is: " + file.getFilePointer());
System.out.println("Line size is: " + file.readLine().length());
System.out.println("Read line is: " + (line = file.readLine()) + " with size: " + line.length());
file.seek(lineNumber);
file.close();
}catch(IOException e){System.out.println(e);}
System.out.println("\n----DEBUG TEST----\n");
}
Understanding the problem
The string -> 1;asd1324;2019-05-22 18:28:56;0;0;
Example: 1;asd1324;2019-05-22 18:28:56;2019-06-01 10:11:16;100;
I want to add them by replacing the String on it's position.
Notes
We will know at all times the size of the string we are going to modify (methods) and act accordingly.
What do I expect from this
Being able to modify the line from a file in Java. Which could be also a general method for this action.
There is no such thing as a text file to a storage media (i.e. HDD, RAM, Flash, etc). Computers always store everything in binary (i.e. bytes). Text file is a human concept.
Taking your example above:
1;asd1324;2019-05-22 18:28:56;0;0;
2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;
Here is how the first 2 lines looks to a computer (in HEX):
313B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A
323B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A
Note how the line is terminated (0A), which the end of line character for most *nix system. On Windows it will be 0D0A (or is it 0A0D?) and on a Mac it will be 0D.
Now to change a line, it must absolutely not be longer than the orignal one otherwise you will end up overwriting onto the start of the next line. Suppose you change the first line from
1;asd1324;2019-05-22 18:28:56;0;0;
to
1;asd1324;2019-05-22 18:28:56;0;01;
you will end up with (first 2 lines):
313B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 30313B
323B6173 64313332 343B3230 31392D30 352D3232 2031383A 32383A35 363B303B 303B0A
so your file will now be read as:
1;asd1324;2019-05-22 18:28:56;0;01;2;asd1324;2019-05-22 18:28:56;0;0;
3;asd1324;2019-05-22 18:28:56;0;0;
4;asd1324;2019-05-22 18:28:56;0;0;
5;asd1324;2019-05-22 18:28:56;0;0;
6;asd1324;2019-05-22 18:28:56;0;0;
You now have 5 lines instead of 6 since you have overwritten the line termination character of the original first line.
With all that said, modifying a text file without rewriting it can be a very tedious and hard task. It way faster and easier to rewrite the whole with your modifications. Otherwise, a military boot camp will feel like walk in the park in comparison.