我有一个存储在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();
}
}
}
但它显示出力量接近.Plz帮助。
答案 0 :(得分: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
答案 1 :(得分:0)
试试这个 - &gt;
val[i++] = readString;
而不是
val [i] = readString;
答案 2 :(得分: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来根据需要逐个获取它们