开发者

Android: Reading doubles from a textfile and put them in an array

开发者 https://www.devze.com 2023-03-22 22:29 出处:网络
I am trying to read values from a text file. There are 6 doubles on each line for the file. I created a getBufReader method

I am trying to read values from a text file. There are 6 doubles on each line for the file. I created a getBufReader method

 public BufferedReader getBufReader(String filename, int rawName){
     if(D) Log.i(TAG, "GETTING FILE");
     BufferedReader bufReader = null;
     // open the file for reading
     InputStream instream = getResources().openRawResource(rawName);

     // if file the available for reading
     if (instream != null) {
       // prepare the file for reading
       InputStreamReader inputreader = new InputStreamReader(instream);
       bufReader = new BufferedReader(inputreader);
       }          
     // close the file again
     try{instream.close();}
        catch (Exception e) {
           if(D) Log.e(TAG, "Unable to Close " + filename);}
     return bufReader;
   }

Each line in the file is supposed to be an entire row in my 2D array. I tried to get a line, tokenize it and put that in the array. But the program keeps crashing. The DDMS Log does does not produce a bunch or errors as I expect

    public void getData(){    
     int rawName = R.raw.values_doubles;
     BufferedReader reader_0 = getBufReader(strData2, rawName );
     //2D array of x rows and y columns to store the data
     //
     double[][] Data_temp = new double[size_x][size_y];
     String line = null;
     StringTokenizer Strtoken;


     for(i=0;i<size_x;i++){           
         try {line = reader_0.readLine();}catch (IOException e) {}
         if(line != null){ 
            Strtoken = new StringTokenizer(line);
            for(j=0;j<size_y;j++){                 
                if (Strtoken.hasMoreTokens()){
                    Data_temp[i][j] = Double.parseDouble(Strtoken.nextToken());
                    CommandsAdapter.add(""+Data_temp[i][j]+"  ");      开发者_如何学C            
                    }}

} CommandsAdapter.add("\n");}}

Please help

Also, I get errors if I don't surroung reader_0.readLine() with try/catch.


I would create a FileInputStream and a Buffered Reader. I am writing this from my lap top so I cannot test this code but this should give you a general idea (my first app saved data on seperate lines in a text file and I could read and write to it using this method) NOTE this requires you to know how many lines are in your file

FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(fis);

Double myArray[][] = new Double [Text_File_Lines][6];
StringBuffer buffer = new StringBuffer();

int x = 0;
int y = 0; 
while (reader.readLine() != -1) { //indicating end of file
  while (reader.nextChar != '\n' ) { //indicating new line
    while (reader.nextChar != ' ' ) { //indicating no space character
      buffer.append(reader.nextChar());
    }
  Double[y][x] = Double.parseDouble(buffer.toString()); //Parse double and add it to array
  buffer = null; //restore buffer to null
  x++;
  }
y++;
}

PLEASE NOTE THIS IS VERY SLOPPY CODE THAT ALMOST DEFINITELY WONT WORK this is just to get you started and I will post my working code when I get home from work.

0

精彩评论

暂无评论...
验证码 换一张
取 消