I have a LONG .txt file that contains almost 6000 lines! Sometimes I need to retrieve the info. in line 5000. Is it possible to start reading from line 5000 rather than sta
As 5000 lines is not so big, and it will do a sequential file access, this simple idea could work:
BufferedReader in = new BufferedReader(new InputStreamReader(fileName));
for (int i = 0; i < 5000 && in.ready; in.readLine()) { }
if (in.ready()) {
// you are at line 5000;
} else {
// the file is smaller than 5000 lines
}
Another idea is to use the bufferedRead.skip(n) method, but for it every line should have the same length. By example, each line having 100 characters, you will need to do:
int ls = System.getProperty("line.separator").length();
in.skip((100 + ls) * 5000);