2013-02-01 59 views
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); 
} 
+0

您是否在LogCat中看到任何错误? –

+0

什么都没有显示 – user1995307

回答

-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是我用来写行的缓冲写入器。我从阅读器读取并写入本地存储的文件。

+0

Wt是什么?我们必须定义它?它给了我一个错误,所以我无法运行它。如果运行它会从最后的位置读取吗?谢谢 – user1995307

+0

不赞成,因为不太好看的while循环。 –

+0

我发现了这个问题。这是写入文件的问题。我们必须以追加模式写入文件,否则所有文件内容都将被重写。代码如下。 OutputStreamWriter out = new OutputStreamWriter(openFileOutput(“myfilename.txt”,MODE_APPEND));阅读我的代码很好,谢谢大家。 – user1995307

-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(); 
} 
+0

谢谢。它显示一个错误“方法hasNext()是未定义的类型BufferedReader”这是什么?如果我们给输入流阅读器添加新内容,并且缓冲阅读器将从下一行读取,或者它将从开始再次开始。我想从最后的位置读取它。 – user1995307

+0

@sachin你有没有给出任何链接? – user1995307

+0

BufferedReader没有任何称为hasNext()的方法。 http://developer.android.com/reference/java/io/BufferedReader.html –

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也应该只包含低字母而不是驼峰。

+0

这不适合我。当我点击按钮时,我什么也看不到 – user1995307

+0

当你的解决方案也不能工作时,不要把负面的评论放在别人的答案中! – user1995307

+0

@ user1995307我的代码没有缺陷,你一定做错了什么。你的意思是TextView没有得到更新?你可能想检查logcat,你没有收到任何异常。 FileNotFoundException不是运行时异常,所以非常有可能找不到“myfilename.txt”,并且它不会崩溃。考虑将FileNotFoundException更改为RuntimeException。另外请不要说其他的代码不起作用,如果你没有用尽它的可能性。 –