0

我有一个存储在 sdcard 中的文本文件。我想将数据从 sdcard 中的文本文件复制到一个字符串数组中。我想将数据存储在字符串数组中,然后在我的 autocompletetextview 中使用该数据。我的代码是:

package com.example.pro2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ReadSettings();

            }
        });
    }
    public void ReadSettings(){

        try{             
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,"comm.txt");        
               FileInputStream fileIS = new FileInputStream(file);          
               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));           
               String readString = new String();                


               String val[] = null;
               for(int i=0;i<5;i++)
               {
               while ((readString = buf.readLine()) != null) {
                   Log.d("line: ", readString);   
                   val[i] = readString;
                   Log.d("values", val[i]);
                   // u better use an ArrayList or you have to check if i < val.size
               }
               }

            } catch (FileNotFoundException e) {          
               e.printStackTrace();          
            } catch (IOException e){             
               e.printStackTrace();          
        }    
    } 
    }

但它显示力量关闭。请帮助。

4

3 回答 3

1

不可能是答案,请在此处更改:

ArrayList<String> val = new ArrayList();
               int i=0;

               while ((readString = buf.readLine()) != null) {
                   Log.d("line: ", readString);   
                   val.add(readString);
                   Log.d("values", val.get(i).toString());
           i++;
                   // u better use an ArrayList or you have to check if i < val.size
于 2012-08-27T07:44:19.523 回答
0

试试这个-->

 val[i++] = readString; 

而不是 val[i] = readString;

于 2012-08-27T07:43:58.600 回答
0

字符串数组不可调整大小,因此请使用字符串缓冲区或 Arraylist,然后将其转换为数组......

您必须预先知道数据的大小,以便您可以用它初始化字符串数组,即

//ArrayList<String>  mArrayList  = new ArrayList<String>();
String abc[] = new String[file size ];

或使用 ArrayList

    mArrayList.add(readString);
    int size =mArrayList.size();
    Object[] m= mArrayList.toArray();

m 包含您的字符串数组,使用 toString 根据需要一一获取它们

于 2012-08-27T07:48:52.320 回答