我有一个存储在 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();
}
}
}
但它显示力量关闭。请帮助。