0

我想从文本文件中读取几行。当我单击下一步按钮时,这些行应该一一显示。我能够将字符串存储在文件中并读取第一行。但是当我尝试使用下一个按钮读取下一行时,它停止工作。谁能告诉我一个解决方案。提前致谢。

我定义了以下内容,

BufferedReader buffreader;      
String line;
String fav;
StringBuilder text;   
InputStream instream;     
String favQuote0;

这是在我的 oncreate 方法中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save);
    StringBuilder text = new StringBuilder();        

    try {
        // open the file for reading
        InputStream instream = openFileInput("myfilename.txt");

        // if file the available for reading
        if (instream != null) {             
            // prepare the file for reading
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);   

            while (( line = buffreader.readLine()) != null) {       
                text.append(line);
                text.append('\n');                  
                fav = text.toString();             
            }
        }
    } catch (IOException e) {}

    TextView tv1 = (TextView)findViewById(R.id.textView2);
    tv1.setText(fav);   
}

这是我的下一个按钮

public void next (View view1) {         
    try {
        // open the file for reading
        InputStream instream = openFileInput("myfilename.txt");

        // if file the available for reading
        if (instream != null) {
            while (( line = buffreader.readLine()) != null) {               
                text.append(line);
                text.append('\n');                  
                fav = text.toString();             
            }
        }
    } catch (IOException e) {}

    TextView tv1 = (TextView)findViewById(R.id.textView2);
    tv1.setText(fav);
}
4

3 回答 3

0

两个答案都是错误的。我将在我的代码下方发布描述。这是正确的做法。

private int mCurrentLine;
private ArrayList<String> mLines;
private TextView mTextView;
private Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_save);

    mCurrentLine = -1;
    mLines = new ArrayList<String>();
    mTextView = (TextView) findViewById(R.id.text_view);
    mButton = (Button) findViewById(R.id.button);

    try {
        readLinesAndSaveToArrayList();
    }

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

    setTextAndIncrement();

    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setTextAndIncrement();
        }
    });
}

private void readLinesAndSaveToArrayList() throws IOException {
    File file = new File("myfilename.txt");

    if (!file.exists()) {
        throw new FileNotFoundException("myfilename.txt was unable to locate");
    }

    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line;

    while ((line = bufferedReader.readLine()) != null) {
        mLines.add(line);
    }

    bufferedReader.close();
}

private void setTextAndIncrement() {
    if (mCurrentLine == mLines.size() - 1) {
        return;
    }

    mCurrentLine++;
    mTextView.setText(mLines.get(mCurrentLine));
}

我所做的是将文件的内容缓存在可扩展的ArrayList. 每一行都将被分配给数组中的一个索引,例如。0、1、2 等等。

mCurrentLine负责阵列中的定位。它从 -1 开始,因为没有当前行。在setTextAndIncrement()它将被设置为0,这在数组中是第一个索引(第一行)。连续调用将增加位置并占用下一行。如果您达到极限,我会检查mCurrentLine是否等于行的最大大小(mLines.size() - 1, a - 1,因为数组从 0 开始而不是 1)。

除此之外,我建议您在方法和变量上使用全长名称;没有必要让它们保持简短,这只会使阅读它们变得更加困难。XML 也应该只包含小写字母而不是驼峰式。

于 2013-02-01T14:24:38.163 回答
-1

尝试这个。这是我为我工作

BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuffer bf = new StringBuffer();
String json = reader.readLine();
try {
do {

    bf.append(json);
    json = reader.readLine();
}while (json != null);
wt.flush();
wt.close();
Log.d("LOG", " read line output "+new String(bf)+" json "+json);
reader.close();     
} catch (Exception e) {
e.printStackTrace(); }

wt 是我用来写行的缓冲写入器。我从阅读器读取并写入本地存储的文件。

于 2013-02-01T12:53:08.790 回答
-2
try {
// open the file for reading
InputStream instream = new FileInputStream("myfilename.txt");

 // if file the available for reading
 if (instream != null) {
 // prepare the file for reading
 InputStreamReader inputreader = new InputStreamReader(instream);
 BufferedReader buffreader = new BufferedReader(inputreader);

  String line;

   // read every line of the file into the line-variable, on line at the time
   while (buffreader.hasNext()) {
 line = buffreader.readLine();
// do something with the line 
}

}
 } catch (Exception ex) {
  // print stack trace.
} finally {
 // close the file.
 instream.close();
 }
于 2013-02-01T12:20:56.053 回答