![](/img/trans.png)
[英]Java GUI - reading textfiles having each line of the text file as it's own string
[英]Java Reading Text File And Saving Each Line As a New String Array
我正在嘗試構建一個將讀取文本文件的應用程序,然后將每一行文本存儲為一個數組列表。
這是我的文本文件:
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
我基本上是在嘗試構建一個Trivia應用程序,該應用程序將從文本文件中選擇每一行,然后將所選行拆分為字符串數組,最后在屏幕上打印一個問題和四個答案。
到目前為止,這是我的代碼:
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 [],這樣我就可以在應用程序按鈕上設置文本了。 有任何想法嗎?
如果要將每一行存儲到字符串數組中,則需要創建“字符串數組的結構”
因此,最有效的選擇是創建將保存字符串數組的List<String[]>
。
您的方法不起作用的原因是,您為每行分配了新值到相同的字符串數組(始終被重寫),並且在循環之后,字符串數組包含最后一行的值。
List<String[]> collection = new ArrayList<String[]>();
String[] temp;
while ((line = br.readLine()) != null) {
temp = line.split(",");
if (temp.length > 0) {
collection.add(temp);
}
}
但是,如果要創建僅包含值的List(我有點困惑),可以使用:
List<String> collection = new ArrayList<String>();
String[] temp;
while ((line = br.readLine()) != null) {
temp = line.split(",");
if (temp.length > 0) {
for (String s: temp) {
collection.add(s);
}
}
}
您可以將所有分割線存儲到ArrayList
:
private ArrayList<String[]> values;
@Override
protected void onCreate(Bundle savedInstanceState) {
values = new ArrayList<String[]>();
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) {
values.add(line.split(","));
//System.out.print(value);
}
br.close();
} catch (IOException e1) {
System.out.println("not good");
}
}
你可以試試這個嗎
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) {
String[] value = line.split(",");
for(int i=0;i<value.length;i++)
System.out.print("*************************************************"+value[i]);
}
br.close();
} catch (IOException e1) {
System.out.println("not good");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.