• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random Access Files  RSS feed

 
Karishma, Dixit
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my input file with 12 lines,i want the 12 th line to be replaced with some string.How do i use a Random access file for this purpose?The lines have numbers 1to 12.Can ran access file recgonise these numbers.Kinddly help.
Thanks.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karishma, Dixit:
How do i use a Random access file for this purpose?The lines have numbers 1to 12.Can ran access file recgonise these numbers.

I must confess the relevance to servlets and jsps escapes me. Oh well. Did you look at the JavaDoc for java.io.RandomAccessFile? Then you must've discovered the readLine() method. If the 12th line is the last line, you can getFilePointer() just before the final readLine(), read the line, modify it, seek() back to the start of the line, then write out your modified string. Finish off by truncating the file using setLength(getFilePointer()).
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!