• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Change a line in text file

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply pete,
May i know how to change a particular value in a line please.

Thanks again.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Check the following link.

Hope it helps.


Rgds,
Rok
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


It's helped me -- too cool! Thanks!
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic