I was experimenting with BufferedReader to read 1st line file to a string. How do I do this? Also how can I read an entire file to a string? How to read a particular line like readline(int line) without iterating through the previous lines?
File namefile = new File(root, ".name");
FileReader namereader = new FileReader(namefile);
BufferedReader in = new BufferedReader(namereader);
You can use
BufferedReader.readLine()to get the first line.Note that the next call to
readLine()will get you the 2nd line, and the next the 3rd line….EDIT:
If you want to specify a specific line, as your comment suggest – you might want to use Apache Commons FileUtils, and use:
FileUtils.readLines(). It will get you aList<String>which you can handle like any list, including getting a specific line. Note that it has more overhead because it reads the entire file, and populates aList<String>with its lines.