Use this code:
// Note: bounds checking left as an exercise
public double getDoubleFromFile(final String filename, final int index)
throws IOException
{
final List<String> list = Files.readAllLines(paths.get(filename),
StandardCharsets.UTF_8);
return Double.parseDouble(list.get(index));
}
However, this slurps the whole file. Why not, if you have to query several times. Another solution:
public double getDoubleFromFile(final String filename, final int index)
throws IOException
{
final Path path = Paths.get(filename);
int i = 0;
String line;
try (
final BufferedReader reader = Files.newBufferedReader(path,
StandardCharsets.UTF_8);
) {
while ((line = reader.readLine()) != null) {
if (index == i)
return Double.parseDouble(line);
i++;
}
return Double.NaN;
}
}