如何从文本文件中读取下一行

时间:2013-02-01 12:10:50

标签: android text-files

我想从文本文件中读取几行。当我点击下一个按钮时,这些行应该逐个显示。我能够将字符串存储在一个文件中并读取第一行。但是当我尝试使用下一个按钮读取下一行时,它会停止工作。任何人都可以告诉我这方面的解决方案。提前谢谢。

我定义了以下内容,

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

3 个答案:

答案 0 :(得分: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也应该只包含低位字母而不是camelCases。

答案 1 :(得分:-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是我用来写行的缓冲区。我从读者那里读到并写入本地存储的文件。

答案 2 :(得分:-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();
 }