Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

write into file.txt

Thread view: 
palmis - 10 Jan 2006 16:14 GMT
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?
TechBookReport - 10 Jan 2006 16:35 GMT
> Hi,
> I have this file.txt
[quoted text clipped - 33 lines]
> "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.

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html

Oliver Wong - 10 Jan 2006 16:38 GMT
>> Hi,
>> I have this file.txt
[quoted text clipped - 37 lines]
> 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
zero - 10 Jan 2006 18:05 GMT
>>> Hi,
>>> I have this file.txt

<snipped>

>>> I want to open it and modify row "overallstatus: 00101100" with
>>> "overallstatus: 00001100" using java commands.
[quoted text clipped - 10 lines]
>
>     - 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.

Signature

Beware the False Authority Syndrome

Roedy Green - 10 Jan 2006 20:22 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 10 Jan 2006 18:15 GMT
>I want to open it and modify row "overallstatus: 00101100" with
>"overallstatus: 00001100" using java commands.
>How can I do?

see http://mindprod.com/applets/fileio.html
and tell it you want to read a file of locale-encoded characters,
buffered and write such a file.
it will generate you a skeleton program.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.