Android:从文本文件中读取双精度并将它们放入数组中
我正在尝试从文本文件中读取值。文件的每行有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建一个 FileInputStream 和一个缓冲读取器。我是在我的笔记本电脑上写的,所以我无法测试这段代码,但这应该给您一个总体思路(我的第一个应用程序将数据保存在文本文件中的单独行上,我可以使用此方法读取和写入它)
注意这需要您知道文件中有多少行
请注意这是非常草率的代码,几乎肯定不会工作这只是为了让您开始,我将发布我下班回家时的工作代码。
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
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.