简体   繁体   中英

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.

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 {

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(); into string[] so i could set the text on my app buttons. 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.

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<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 :

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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