Java读取文本文件并将每一行保存为新的字符串数组

标签 java android arrays string file

我正在尝试构建一个应用程序,它将读取文本文件,然后将每行文本存储为数组列表。

这是我的文本文件:

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");

        }

    }

}

问题是应用程序只打印文本文件的最后一行

<小时/>

谢谢你的回答,对我很有帮助! 这是我到目前为止的代码:

公共(public)类 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<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);
      }
   }
}

关于Java读取文本文件并将每一行保存为新的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15852813/

相关文章:

java - 如何创建使用方法的链接列表数组

c++ - vector n 维 vector 本身具有特定的数据类型

java - 过于激进的垃圾收集支配着 CPU

java - Java 的免费/开源测试生成器?

JSP 中的 java.util.Map.contains() 方法调用

java - Java 中的字符串相等

android - 如何等待截击响应完成它在 intentservice 中的工作?

android - 无法在 Android 中将 XMPP 服务器与 ASmack 连接

Android:java.lang.IllegalStateException:必须连接 GoogleApiClient

javascript - 如何根据对象列表的一个或两个键来比较和分离对象列表的数组