Frage

I am trying to do a bilinear interpolation and for that I have to read txt file which contains a NxM dimension.

I need to read the value for a specyfic row and column, I think on two options:

  • Read directly that value from txt file knowing at which column and row attempt. Any idea if its possible?

  • And the other, read all the file and store on a 2nd array, then read needed value pointing to the exact column and row on 2nd array.

The file separate each value with a doble space. I assume that file have to be stored on assets no? I will thank fot any code or documentation (I do not find out)

Thanks in advance ;)

War es hilfreich?

Lösung 2

Hi finally I read a bidimensional array as:

public double[][] readArray2D(Context c, String file,int rows,int cols) throws IOException {

    double [][] data = new double[rows][cols];
    int row = 0;
    int col = 0;
    BufferedReader bufRdr  = null;
    try {
        bufRdr = new BufferedReader(new InputStreamReader(c.getAssets().open(file)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    String line = null;
    //read each line of text file
    try {
        while((line = bufRdr.readLine()) != null && row < data.length)
        {
            StringTokenizer st = new StringTokenizer(line," ");
            while (st.hasMoreTokens())
            {
                //get next token and store it in the array
                data[row][col] = Double.parseDouble(st.nextToken());
                col++;
            }
            col = 0;
            row++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return data;
}

Thanks for all ;)

Andere Tipps

Your app will definitely run faster if you load the txt file into a 2D array before working with the values. Opening something from persistent storage takes a lot longer than looking it up in memory.

Depending on the size of the array you may run out of memory, that's when you will need to be a bit more clever about which parts of the file you read into memory to work with at each stage.

There are plenty of questions on StackOverflow about reading a 2D array from a text file in Java and it should be similar in Android.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top