write into file.txt

P

palmis

Hi,
I have this file.txt

sectionId: 10
overallstatus: 00101100
numberOfEvents: 0
eventCode: 0
eventSeverity: 0
eventId: 0
eventTimeStamp: 0
numberParameters: 0
numberDetMonitData: 0
originAddress: 1
destinationAddress: 554
LRUStatus: 00000101
LRUStatus: 00001001
LRUStatus: 00001101
LRUStatus: 00010001
LRUStatus: 00011001
FunctionStatus: 00000101
FunctionStatus: 00001001
FunctionStatus: 00001101
FunctionStatus: 00010101
FunctionStatus: 00011001
FunctionStatus: 00011101
FunctionStatus: 00100001
FunctionStatus: 00100101
FunctionStatus: 00101001
SWRUStatus: 00000101
SWRUStatus: 01000001
SWRUStatus: 01000101
parameterType: 0
characterStringLength: 0

I want to open it and modify row "overallstatus: 00101100" with
"overallstatus: 00001100" using java commands.
How can I do?
 
T

TechBookReport

palmis said:
Hi,
I have this file.txt

sectionId: 10
overallstatus: 00101100
numberOfEvents: 0
eventCode: 0
eventSeverity: 0
eventId: 0
eventTimeStamp: 0
numberParameters: 0
numberDetMonitData: 0
originAddress: 1
destinationAddress: 554
LRUStatus: 00000101
LRUStatus: 00001001
LRUStatus: 00001101
LRUStatus: 00010001
LRUStatus: 00011001
FunctionStatus: 00000101
FunctionStatus: 00001001
FunctionStatus: 00001101
FunctionStatus: 00010101
FunctionStatus: 00011001
FunctionStatus: 00011101
FunctionStatus: 00100001
FunctionStatus: 00100101
FunctionStatus: 00101001
SWRUStatus: 00000101
SWRUStatus: 01000001
SWRUStatus: 01000101
parameterType: 0
characterStringLength: 0

I want to open it and modify row "overallstatus: 00101100" with
"overallstatus: 00001100" using java commands.
How can I do?

If the file is as short as you've shown, then I'd read the contents of
the file into a single string, do the replace on that and then write the
string back out as a file.
 
O

Oliver Wong

TechBookReport said:
If the file is as short as you've shown, then I'd read the contents of the
file into a single string, do the replace on that and then write the
string back out as a file.

Agreed, but if the file may turn out to be longer, an alternative
solution might be to read one line at a time. Check the line you've read in
to see if it's the line you want to replace (and if so, then go ahead and
replace it). Then write out the line, also one at a time.

- Oliver
 
Z

zero

Agreed, but if the file may turn out to be longer, an alternative
solution might be to read one line at a time. Check the line you've
read in to see if it's the line you want to replace (and if so, then
go ahead and replace it). Then write out the line, also one at a time.

- Oliver

to the OP:

Just to make sure you understand the basics here, a word of explanation.
You cannot just replace one line in a simple text file, you need to
recreate the whole file because you don't know how long a line is. You
could make sure all lines are of equal size (for example append spaces),
forward to the correct line, and overwrite that. This is usually not
appropriate for text files though.

Mostly , for plain text files you'll need one of the schemes mentioned by
the other posters.
 
R

Roedy Green

You
could make sure all lines are of equal size (for example append spaces),
forward to the correct line, and overwrite that. This is usually not
appropriate for text files though.

and even then you have the problem of encoding. If you are using
UTF-8, for example, some characters can take up more than one byte.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top