• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A problem with reading files

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}

------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic