【问题标题】:How to read a .txt document line by line in android?如何在android中逐行读取.txt文档?
【发布时间】:2015-03-20 04:00:08
【问题描述】:

在此代码中,仅读取 .txt 文件中输入的最后一个单词。满足条件时,我正在调用相同的活动。我应该对代码进行哪些更改,以便它读取一个单词,再次调用相同的活动,然后读取一个新单词?

public class Page extends Activity {

    Button b;
    TextView t;
    EditText e;
    int i = 0;
    String str2;
    String w, x=null;

    static BufferedReader c = null;
    static BufferedReader ic = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page);
        b = (Button) findViewById(R.id.benter);
        t = (TextView) findViewById(R.id.tv);
        e = (EditText) findViewById(R.id.et);
        InputStream f = getResources().openRawResource(R.raw.incorrect);
        ic = new BufferedReader(new InputStreamReader(f));

        try {
            while ((w = ic.readLine()) != null) {
                    t.setText(w);
                    b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            str2 = e.getText().toString();

                            if (str2.equals(x)) {
                                Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();


                                Intent open = new Intent(Page.this, Page.class);
                                startActivity(open);

                            } else {
                                Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
                                e.setText("");
                            }
                        }
                    });


                }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: android readline


    【解决方案1】:

    您可以使用此代码逐行读取文件

        try {
            FileReader fileReader = new FileReader("FilePath");
            BufferedReader reader = new BufferedReader(fileReader);
            String data = null;
            StringBuilder builder = new StringBuilder();
            while ((data = reader.readLine()) != null) {
                builder.append(data);
            }
    
            System.out.println(builder.toString());
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    在您的代码中,您的 onclick 侦听器有问题..请将 onclick 侦听器置于 while 循环之外..

    【讨论】:

      【解决方案2】:

      你的 onClickListener() 有错误:

      public class Page extends Activity {
      
      Button b;
      TextView t;
      EditText e;
      int i = 0;
      String str2;
      String w, x=null;
      
      static BufferedReader c = null;
      static BufferedReader ic = null;
      
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.page);
          b = (Button) findViewById(R.id.benter);
          t = (TextView) findViewById(R.id.tv);
          e = (EditText) findViewById(R.id.et);
          InputStream f = getResources().openRawResource(R.raw.incorrect);
          ic = new BufferedReader(new InputStreamReader(f));
      
          try {
              while ((w = ic.readLine()) != null) {
                      t.setText(w);
                      b.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
                              str2 = e.getText().toString();
                              // it was str2.equals(x) nut x is null, replace w with x
                              if (str2.equals(w)) {
                                  Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
      
      
                                  Intent open = new Intent(Page.this, Page.class);
                                  startActivity(open);
      
                              } else {
                                  Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
                                  e.setText("");
                              }
                          }
                      });
      
      
                  }
      
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      }
      

      此代码将为您工作

      【讨论】:

        【解决方案3】:

        您可以为此使用 Apache Commons I/O 库。它非常简单。并保持您的代码美观。为什么要从 assets 获取 .txt 文件?

        AssetManager am = context.getAssets();
        InputStream fs = am.open("a.txt");
        List<String> lines = IOUtils.readLines(file,"UTF-8");
        

        【讨论】:

          【解决方案4】:

          您可以将文本文件保存在项目的“Assets”文件夹中,并使用以下代码在 java 类中检索该文件

           try {
              reader = new BufferedReader(
                      new InputStreamReader(getAssets().open("YOUR_TEXT_FILE.txt")));
              StringBuilder total = new StringBuilder();
              String line;
              while ((line = reader.readLine()) != null) {
                  total.append(line);
              }
              message=total.toString();
              System.out.println(message);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          

          之后,您在字符串消息中拥有该文件

          【讨论】:

            【解决方案5】:

            您的程序正在读取最后一行,因为您每次调用 readLine() 方法时都会覆盖“w”变量。您应该做的是将您的 'w' 变量设置为空,并使用 readLine() append 以保留文本文件中已读取的所有行,然后调用setText() 一次将所有内容放在您的视图中。如果不附加,您实际上是在每次运行 while 循环时覆盖“w”变量,这就是您从文本文件接收最后一行的原因。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-02-16
              • 2012-09-05
              • 1970-01-01
              相关资源
              最近更新 更多