How to modify a TXT file ?
Hi,
I have a file named "hello.txt" built like this:
toto
1
0
1
tata
1
1
0
In fact, toto is followed by 3 attributes.
I would like to turn the 0 of toto in 1.
HOW CAN I DO THAT WITHOUT WRITING THE ENTIRE FILE ?
HOW CAN I MODIFY ONLY ONE LINE IN A FILE ?
[345 byte] By [
navatel] at [2007-9-26 3:13:07]

You will have to use java.io.RandomAccessFile. This allows a file to be opened in read/write mode, you can then seek() to a known offset, or in your case (perhaps) read until a certain condition is met, then you can write. Be careful though, this is most likely to be used with binary files, not text files. You will not be able to insert bytes into the middle of the file, you will only be able to overwrite bytes.
If you want to convert a 0 to a 1, then you are OK, but you could not change 0 to 87 (say).
this code will change the 10th byte of a file to a '0'
import java.io.*;
public class RaTest {
public static void main(String[] args) {
try {
RandomAccessFile fileHandle = new RandomAccessFile ("a.txt","rw");
fileHandle.seek(10);
fileHandle.writeByte(48);
fileHandle.close();
} catch (Exception e) {/* whatever */}
}
}