Android:从文本文件中读取双精度并将它们放入数组中

发布于 2024-11-26 18:02:24 字数 1797 浏览 0 评论 0原文

我正在尝试从文本文件中读取值。文件的每行有 6 个双精度值。 我创建了一个 getBufReader 方法

 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;
   }

文件中的每一行都应该是我的 2D 数组中的一整行。我试图获取一行,将其标记化并将其放入数组中。但程序总是崩溃。 DDMS 日志不会像我预期的那样产生一堆或错误

    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]+"  ");                  
                    }}

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

请帮忙

另外,如果我不使用 try/catch 围绕 reader_0.readLine() ,我会收到错误。

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]+"  ");                  
                    }}

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

Please help

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-12-03 18:02:24

我将创建一个 FileInputStream 和一个缓冲读取器。我是在我的笔记本电脑上写的,所以我无法测试这段代码,但这应该给您一个总体思路(我的第一个应用程序将数据保存在文本文件中的单独行上,我可以使用此方法读取和写入它)
注意这需要您知道文件中有多少行

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++;
}

请注意这是非常草率的代码,几乎肯定不会工作这只是为了让您开始,我将发布我下班回家时的工作代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文