aspose file tools*
The moose likes I/O and Streams and the fly likes Change a line in text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Change a line in text file" Watch "Change a line in text file" New topic
Author

Change a line in text file

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi all.
I have tried a lot before to change a particular line in a text file. I have also posted here some of the code also long back. I got suggestions to write the contents to temp file and rename it and all that. Now have observed that, there is an option in notepad itself to replace the text. How does that work. Is it possible to do through java the same.

Thanks and Regards
alexander
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
there is an option in notepad itself to replace the text

And you understand of course, that if you do this in Notepad, all you're replacing is the text in memory, and that this has nothing to do with changing text in files, right? If with Notepad you do want to save the text changes in the file itself, you'll still have to overwrite the entire file. So how is this different or better than Java?
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks a lot for your reply pete,
May i know how to change a particular value in a line please.

Thanks again.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

adeeb alexander wrote:May i know how to change a particular value in a line please.

Well, you already know that you have to read in the data and write out the changed data to a new file. So your question must be about changing a line once you have it in memory. Given that, it's an extremely unspecific question. There could be all kinds of answers, such as unspecific answers like "Use methods from the String and StringBuilder classes". But if you posted a more specific question, you might get an answer which is more useful.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
adeeb alexander wrote:Thanks a lot for your reply pete,
May i know how to change a particular value in a line please.


I'm getting the feeling that you think that there is a secret that allows you to change a small value of the file on the disk without having to write out the entire file, but if we're talking about text, and not fixed-length records, this isn't going to happen. To change a "particular value in a line" you read in the file, change the text in memory, and then write out the file, the entire file. I'm not sure what about this process you're not sure of.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks for your replies.
I got you guyz. But the problem lies when the file size is more. Example its some 700 MB. Then consider that this file have to be changed at regular intervals of time, then i have to load the whole file at once in memory and change the contents. I just wanted to know that isn't there another way out.


Thanks and regards
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
adeeb alexander wrote:Thanks for your replies.
I got you guyz. But the problem lies when the file size is more. Example its some 700 MB. Then consider that this file have to be changed at regular intervals of time, then i have to load the whole file at once in memory and change the contents. I just wanted to know that isn't there another way out.

If you have to deal with retrieving, changing and storing a large amount of data, it's starting smell to me like you may need a database.
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

Check the following link.

Hope it helps.


Rgds,
Rok


SCJP, SCWCD
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rok Štelcer wrote:
Check the following link.
Hope it helps.

It's helped me -- too cool! Thanks!
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.

Rok Štelcer wrote:
Check the following link.
Hope it helps.

The code from the above link is here.



Can anybody please let me know that how to find different Hex code for different sizes as int the above line its given as static int length = 0x8FFFFFF; // 128 Mb


Thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Change a line in text file
 
Similar Threads
JAI writing and ammending tEXT header
Text substitution in text files
Stream for reading and deleting line
having help menuitem
To print MAX and MIN line from a text file