0

How do I fetch a value corresponding to i (if i=5, I must get 57.05698808926067) from a text file myFile.txt ? The values may continue till 25000.

0->37.6715587270802
1->40.02056806304368
2->351.65161070935005
3->54.74486689415533
4->86.12063488461266
5->57.05698808926067
6->0.0
7->56.078343612293374
Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Shrikant Kakani
  • 1,511
  • 2
  • 17
  • 37

4 Answers4

1

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;
    }
}
fge
  • 119,121
  • 33
  • 254
  • 329
0

Read the file line by line and check characters before "->" and parse them to int and compare with i. Then get the values after "->".

You could also just read line by line and increment the value of an index variable every time you read a line and when the index is equal to i, get the string after "->".

anirudh
  • 4,116
  • 2
  • 20
  • 35
0

This simple way is to read line by line til you get to your line

BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int i=0;
while ((line = br.readLine()) != null) {
   if (i == muLineNumber){
       // process the line. 
       break;
   }

   i++;
}
br.close();

If the size of each line const, you can use BufferedReader.skip

If the file size of small, use FileUtils.readLines

Choose what is best for you

Mzf
  • 5,210
  • 2
  • 24
  • 37
  • `FileUtils.readLines()` is basically superseded by Java 7's `Files.readAllLines()` – fge Mar 18 '14 at 08:00
  • Heh, Java 8 is out today (Thu Mar 18th) and people still don't use the new Java 7 file API ;) It is miles better than `File`. – fge Mar 18 '14 at 08:03
  • You may want to read [this](http://fgaliegue.blogspot.com/2014/03/working-with-files-java-6-versus-java-7.html), in the hope you will find it useful – fge Mar 18 '14 at 08:05
  • java 7 highlight : http://stackoverflow.com/questions/213958/new-features-in-java-7 – Mzf Mar 18 '14 at 08:09
0

Since 25'000 lines is not that huge, I would load the whole file in an array, and use i as an index into the array. If, however, I had harsh constraints on memory usage, I would use an RandomAccessFile, set position somewhere around i*average-line-length, find the next '\n', then read the index, if the index is the one that I was seeking, I would read the rest of the line, otherwise, move up if the index is greater or down if it's smaller, and repeat the process.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97