This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help needed  RSS feed

 
Ranvir Johal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys,
I just want to know how to read from a file a line at a time in java servet or in a normal java file. like when we start program again i will automatically start read at next line and so on. and in the end i will comes again on first line. please guys help me out.
thanks in advance [color=darkred]
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranvir, welcome to JavaRanch!

In the future, you'll get more responses if you put more information in the header than "help needed". People often figure out from the header whether they will be able to answer or not, and just skip past the rest.

OK, to read from a file, you should use a FileReader. To read one line at a time, you need a Buffered Reader. So to read one line at a time, you need a FileReader wrapped by a BufferedReader. Check the Java I/O Tutorial for more info.

If you want to read one line per run of your program, you'll need to keep a counter somewhere. You could store that in a database, or in another file on the file system. If you were clever about it, you might even store it in a header of the file with the lines in it. However, that could get a bit tricky.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!