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]
# 1
use randomaccessfilebut you wont be able to change 0 into 12 for exemple
zuby1 at 2007-6-29 11:22:10 > top of Java-index,Archived Forums,Java Programming...
# 2
Can you explain me a little bit more ?
navatel at 2007-6-29 11:22:10 > top of Java-index,Archived Forums,Java Programming...
# 3

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).

rob_canoe2 at 2007-6-29 11:22:10 > top of Java-index,Archived Forums,Java Programming...
# 4
OK, does anyone have an example of code for that ?
navatel at 2007-6-29 11:22:10 > top of Java-index,Archived Forums,Java Programming...
# 5

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 */}

}

}

rob_canoe2 at 2007-6-29 11:22:10 > top of Java-index,Archived Forums,Java Programming...