Reading a specific line from a text file in Java

后端 未结 9 750
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 07:34

Is there any method to read a specific line from a text file ? In the API or Apache Commons. Something like :

String readLine(File file, int lineNumber)
         


        
9条回答
  •  余生分开走
    2020-11-27 08:01

    Not that I'm aware of.

    Be aware that there's no particular indexing on files as to where the line starts, so any utility method would be exactly as efficient as:

    BufferedReader r = new BufferedReader(new FileReader(file));
    for (int i = 0; i < lineNumber - 1; i++)
    {
       r.readLine();
    }
    return r.readLine();
    

    (with appropriate error-handling and resource-closing logic, of course).

提交回复
热议问题