BufferedReader的循环语句不起作用

时间:2014-03-11 22:19:03

标签: android bufferedreader bufferedinputstream

我似乎忽略了一些非常明显的循环,但在目前的状态下,它并没有将eula文本文件的内容发布到警告对话框中。有人看到我忽视的任何东西吗?文本文件中有21行。谢谢!

Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.eula);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));

    String key1 = preference.getKey();
    //if the user click on the Legal Notices preference, display the license
    if (key1.equalsIgnoreCase("prefEULA")){
    String eulaContent = null;
    try {

        while ((reader.readLine()) != null)
            for(int i=0;i<21;i++){
        {
             eulaContent = reader.readLine();

    }

    }
    }   catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Builder eulaDialog = new AlertDialog.Builder(this);
    eulaDialog.setTitle("End-User Licence Agreement");
    if (eulaContent!=null){
    eulaDialog.setMessage(eulaContent);
    eulaDialog.show();                

    }            

    }

2 个答案:

答案 0 :(得分:2)

更新: 问题是方法openRawResource。它在字符串内容上失败

删除for循环并尝试使用+ =运算符

Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.eula);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));

String key1 = preference.getKey();
//if the user click on the Legal Notices preference, display the license
if (key1.equalsIgnoreCase("prefEULA")){
String eulaContent = "";
try {

    while ((reader.readLine()) != null)
    {
         eulaContent += reader.readLine();

}
}   catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Builder eulaDialog = new AlertDialog.Builder(this);
eulaDialog.setTitle("End-User Licence Agreement");
if (eulaContent!=null){
eulaDialog.setMessage(eulaContent);
eulaDialog.show();                

}            

}

答案 1 :(得分:0)

我真的很想使用我已经编写过的循环,并且能够得到一些建议让我走上正轨。我最终得到了下面的代码,这真的很棒!对循环进行一些调整,包括为要追加的缓冲数据添加一个空String对象。此外,...+ "\n";位将新行添加回缓冲数据。

 Resources res = getResources();  
 InputStream in_s = res.openRawResource(R.raw.eula);  
 BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));  

        String key = preference.getKey();  
        //if the user click on the Legal Notices preference, display the license  
        if (key.equalsIgnoreCase("prefEULA")){  
        String eulaContent = "";  
        try {  

            String line = null;  
            while ((line = reader.readLine()) != null) {  

            eulaContent += line + "\n";
        }  


        }   catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        Builder eulaDialog = new AlertDialog.Builder(this);  
        eulaDialog.setTitle("End User License Agreement");  
        if (eulaContent!=null){  
        eulaDialog.setMessage(eulaContent);  
        eulaDialog.show();                  

        }              

        }