• Post Reply Bookmark Topic Watch Topic
  • New Topic

A problem with reading files

 
chenw
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When openning a file, we can use "readLine()" to read lines of this file one by one .Could I read a specific line such as the sixth line immediately?
 
Bill Hause
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't immediately read the sixth line without first reading the lines before it. This is because you must count the new line characters in the file to know what line you are on.
E.g. after reading five '\n' characters you know you are on line six.
You could write a method like the one below but it still needs to read each line.
String readLine(int n) {
for (int i=0; i<(n-1); i++)
readLine();
return readLine();
}

------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!