Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modifying the contents of a file  RSS feed

 
Meenakshi Sundaram Palaniappan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,
can anyone help me out to modify or delete a particular line stored in a file without rewriting the entire thing....





Thanks in advance.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the new line is exactly the same length as the line being replaced, it can't be done.
 
Meenakshi Sundaram Palaniappan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:Unless the new line is exactly the same length as the line being replaced, it can't be done.


Thank you Richard,
but its like i am designing a contact management system for my project wherein i have to provide options for changing the contents stored and i cant use databases as would be against the project requirements... so if there is any way to change please tell me....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenakshi Sundaram Palaniappan wrote:
Richard Tookey wrote:Unless the new line is exactly the same length as the line being replaced, it can't be done.


Thank you Richard,
but its like i am designing a contact management system for my project wherein i have to provide options for changing the contents stored and i cant use databases as would be against the project requirements... so if there is any way to change please tell me....


You already know how. You're clearly aware of the option of rewriting the whole file, since you mentioned it in your first post. (Actually you can get away with only rewriting starting with the change and going the rest of the way to the end, but that's usually more effort than it's worth.) The other option, as Richard indicated, is to make the replacement line exactly the same length as the original. This is generally not practical.
 
Meenakshi Sundaram Palaniappan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I dont think i got the exact solution for the problem.....
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenakshi Sundaram Palaniappan wrote:
I dont think i got the exact solution for the problem.....


I thought it had been made clear that the solution to the problem is not to try to edit the content of the file. In my view you need to re-design your application so that it does not need to. Consider the user of a database.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenakshi Sundaram Palaniappan wrote:
I dont think i got the exact solution for the problem.....


What part do you not understand? Read the whole file, write the whole file, changing the appropriate lines along the way.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is very large and therefore has a large overhead associated with rewriting it then you could consider appending changes in a diff format to a separate file. Of course you then have the problem of applying all the diff changes every time you want to read the file.

You'd need to do some experimentation but I suspect the file would have to be pretty large before this approach would make any sense.
 
Meenakshi Sundaram Palaniappan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant work with databases since my project requirement says i have to work with files..... I am now successful with Jeff's idea of reading the file ,making the changes and writing it back......Thank you guys !!!You all were very helpful!!!

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is too large to fit into memory, the following technique can also be used:
1) create a temporary file (check java.io.File for some useful methods)
2) read a line from the source file
3) write the line to the temporary file (modified or skipped as needed)
4) repeat steps 2-3 until done
5) delete the original file
6) rename the temporary file to the original file
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!