有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

这是我的文本文件:

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

        }

    }

}

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


谢谢你的回答,它真的帮助了我! 这是我目前的代码:

公共类SQL扩展活动{

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=newarraylist(); 转换为字符串[],以便我可以设置应用程序按钮上的文本。有什么想法吗


共 (3) 个答案

  1. # 1 楼答案

    你能试试这个吗

    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");
    
        }
    
    }
    
  2. # 2 楼答案

    如果要将每行存储到字符串数组中,则需要创建“字符串数组结构”

    因此,最有效的选择是创建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);
          }
       }
    }
    
  3. # 3 楼答案

    您可以将所有拆分的行存储到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");
        }
    }