在android中逐行读取文本文件

时间:2014-10-07 02:54:43

标签: android text-files line-by-line

问题可能很熟悉,甚至可能被标记为重复。我曾尝试阅读与我类似的其他问题并试图这样做,可能就像复制它(xD),但无济于事。

我是Android编程的新手,我正在尝试阅读文本文件。我能够读取整个文本文件,但我想要的是逐行读取文本文件。然后单击下一步按钮以显示下一行。我应该怎么做?

这就是我现在所拥有的。 。

public class QuestionActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);

    text = (TextView) findViewById(R.id.textView1);
    try {
        in = this.getAssets().open("hhquestion.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    reader = new BufferedReader(new InputStreamReader(in));
    try {
        line = reader.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    text.setText(line);
    Button next = (Button) findViewById(R.id.btnNext);
    next.setOnClickListener(this);

} //onCreate

public void onClick(View v){
    try {
        line = reader.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (line != null){
        text.setText(line);
    } else {
        //you may want to close the file now since there's nothing more to be done here.
    }
    } //onClick

} //main class

我已经尝试了this question的答案,因为这是我问题的最近解决方案,但是当我运行项目时,应用程序正在关闭。 谢谢。

logcat的:

10-07 00:06:06.199: E/AndroidRuntime(1907): FATAL EXCEPTION: main
10-07 00:06:06.199: E/AndroidRuntime(1907): Process: asp.ons.scads, PID: 1907
10-07 00:06:06.199: E/AndroidRuntime(1907): java.lang.RuntimeException: Unable to start activity ComponentInfo{asp.ons.scads/asp.ons.scads.QuestionActivity}: java.lang.NullPointerException
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.os.Looper.loop(Looper.java:136)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at java.lang.reflect.Method.invoke(Method.java:515)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at dalvik.system.NativeStart.main(Native Method)
10-07 00:06:06.199: E/AndroidRuntime(1907):     Caused by: java.lang.NullPointerException
10-07 00:06:06.199: E/AndroidRuntime(1907):     at asp.ons.scads.QuestionActivity.onCreate(QuestionActivity.java:43)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.Activity.performCreate(Activity.java:5231)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-07 00:06:06.199: E/AndroidRuntime(1907):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-07 00:06:06.199: E/AndroidRuntime(1907):     ... 11 more

2 个答案:

答案 0 :(得分:0)

一切都很好..但是当你点击下一个按钮时显示行你需要setonClickListener()来获得下一个按钮。

    public class QuestionActivity extends ActionBarActivity {
    static InputStream inputStream;
    static InputStreamReader inputreader;
    static BufferedReader bufferedreader;
    String data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        TextView textView = (TextView)findViewById(R.id.hellotxt);
        Button next=(Button) findViewById(R.id.next); // next button id for suppose..

        inputStream = ctx.getResources().openRawResource(resId);

        inputreader = new InputStreamReader(inputStream);

        bufferedreader = new BufferedReader(inputreader);

        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
             try 
             {
                 while (( line = bufferedreader.readLine()) != null) 
                 {
                  data+=line+"\n";
                 }
              } 
           catch (IOException e) {}

              textView.setText(data);
            }
          });

    } //onCreate

  } //main class

答案 1 :(得分:0)

  

我能够读取整个文本文件,但我想要的是逐行读取文本文件。然后单击下一步按钮以显示下一行。我应该怎么做?

在当前循环中,您将每行保存到line变量,然后使用换行符将其附加到stringBuilder

您可以在每次迭代时将stringBuilder添加到某种数组,而不是将其附加到line,然后在每次用户点击下一个按钮时访问下一个元素。

或者,您可以使用返回的stringBuilder以及添加到每行末尾的换行符执行某些操作,例如split(这实际上与上面相同,但使用您的已编写代码)。因此,在onCreate结束时,您可以调用split来获取由换行符分割的字符串数组:

String[] lines = data.toString().split("\\n");

然后每次用户单击按钮时,您可以递增计数器并获取计数器处元素的行,或者您可以稍微复杂一点并在读取时从数组中删除该元素(尽管您在这种情况下可能不需要数组。)