简体   繁体   English

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

[英]Java Reading Text File And Saving Each Line As a New String Array

I'm trying to build an app that will read a text file , then store each line of text as an array list. 我正在尝试构建一个将读取文本文件的应用程序,然后将每一行文本存储为一个数组列表。

this is my text 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 

i'm basically trying to build an trivia app that will choose each line from the text file, then split the selected line into string array ,and finally print on the screen one question and four answers. 我基本上是在尝试构建一个Trivia应用程序,该应用程序将从文本文件中选择每一行,然后将所选行拆分为字符串数组,最后在屏幕上打印一个问题和四个答案。

this is my code so far: 到目前为止,这是我的代码:

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

        }

    }

}

the problem is that the app only print the last line of the text file 问题是该应用程序仅打印文本文件的最后一行


thank you for the answers, it really helped me! 谢谢您的回答,对我有很大帮助! this is my code so far: 到目前为止,这是我的代码:

public class QuestionSql extends Activity { 公共类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");

    }

}

} }

now, i need to convert the :collection = new ArrayList(); 现在,我需要转换:collection = new ArrayList(); into string[] so i could set the text on my app buttons. 转换为string [],这样我就可以在应用程序按钮上设置文本了。 any ideas? 有任何想法吗?

If you want to store each line into string array you need to create "structure of string arrays" 如果要将每一行存储到字符串数组中,则需要创建“字符串数组的结构”

So the most efficient choice is to create List<String[]> that will hold your string arrays. 因此,最有效的选择是创建将保存字符串数组的List<String[]>

Reason why your approach didn't work was that you assigned for each line new values to same string array(which were rewritten, always) and after loop your string array contained values of last line. 您的方法不起作用的原因是,您为每行分配了新值到相同的字符串数组(始终被重写),并且在循环之后,字符串数组包含最后一行的值。

List<String[]> collection = new ArrayList<String[]>();
String[] temp;
while ((line = br.readLine()) != null) {
   temp = line.split(",");
   if (temp.length > 0) {
      collection.add(temp);
   }
}

But if you want to create List that will contain only your values(i'm little confused) you can use: 但是,如果要创建仅包含值的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);
      }
   }
}

You could store all split lines into an ArrayList : 您可以将所有分割线存储到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");
    }
}

Can you try this 你可以试试这个吗

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM