• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading a file by lines

 
Guadalupe Ortiz
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I works a lot with java, but I still have not clear anything about files. Can anybody send me a few lines describing how to read a text file by lines and how could I delete any of the lines or change it content or add a new line?

Thank-you very much
Gobellot
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this a long time ago, i hardly remember what it does, it needs a little bit modifications to suit your need, but it�ll help you to get started. Good Luck.


[ May 15, 2004: Message edited by: Vicken Karaoghlanian ]
 
Guadalupe Ortiz
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you very much. I have tried the code and now I know how to read a file by lines and how to append information to the end of a file, but I still have one doubt. I got a file and I desire to modify the content of a determinated file (simply delete that line in instead writting a new information at that line) �Is there anything similiar to the "lineNumberReader" for writting in a line number? If not, is there any other function for that?
thanks again for your help
Gobellot
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it is possible to modify the original file directly. You can however, read the input file line-by-line then write it back to another output file with your modified lines, afterwards you can simply overwrite the input file with the output file. This is the only approach i am aware of, and i am almost certain that it can't be done any other way.

Tip: If you are dealing with small sized files, you can read the entire file into memory, and do your entire modifications there; this will improve the performance of your application.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to I/O And Streams...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic