我正在尝试构建一个将读取文本文件的应用程序,然后将每一行文本存储为数组列表。
这是我的文本文件:
1 , Where is the white house? , Paris , Amsterdam , New York , Washington
2 , The Sopranos Is a..? , Italian Food , Tv series , Kind of Knife , A Book
3 , The Capital City Of Brazil is? , Rio de Janeiro, Amsterdam , Brazilia , Washington
4 ,Who Invanted The Phone ?, Alexander Graham Bell, Albert Einstein , Pinokio , Snoop Doog
我基本上是在尝试构建一个琐事应用程序,它将从文本文件中选择每一行,然后将选定的行拆分为字符串数组,最后在屏幕上打印一个问题和四个答案。
到目前为止,这是我的代码:
public class QuestionSql extends Activity {
private String[] value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
readFile();
}
private void readFile() {
// TODO Auto-generated method stub
AssetManager manger;
String line = null;
try {
manger = getAssets();
InputStream is = manger.open("text.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
value = line.split(",");
//System.out.print(value);
}
br.close();
} catch (IOException e1) {
System.out.println("not good");
}
}
}
问题是应用程序只打印文本文件的最后一行
谢谢你的答案,它真的帮助了我!到目前为止,这是我的代码:
公共类 QuestionSql 扩展 Activity {
private String[] value;
private List<String[]> collection;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.highscore);
readFile();
convertListToString()
}
private void convertListToString() {
value = collection.toArray(new String[collection.size()]);
}
private void readFile() {
// TODO Auto-generated method stub
AssetManager manger;
String line = null;
collection = new ArrayList<String[]>();
try {
manger = getAssets();
InputStream is = manger.open("text.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
value = line.split(",");
collection.add(value);
}
br.close();
} catch (IOException e1) {
System.out.println("not good");
}
}
}
现在,我需要转换 :collection = new ArrayList(); 进入 string[] 以便我可以在我的应用程序按钮上设置文本。有任何想法吗?