【问题标题】:Android/Java text file read problem [duplicate]Android/Java文本文件读取问题[重复]
【发布时间】:2011-11-23 03:14:56
【问题描述】:

我目前正在尝试使用我这样标注的 InputStream 从 (res/raw) 读取文件:

InputStream mStream = this.getResources().openRawResource(R.raw.my_text_file_utf_8);

然后我将其放入此方法中以返回值:

public List<String> getWords(InputStream aFile) {

List<String> contents = new ArrayList<String>();
try {
  BufferedReader input =  new BufferedReader(new InputStreamReader(aFile));          
  try {
    String line = new String();//not declared within while loop
    while ((line = input.readLine()) != null ){              
        contents.add(line);
    }
  }
  finally {
    input.close();
  }
}
catch (IOException ex){
  ex.printStackTrace();
}

return contents;

}

我的问题:它会读取所有的值,但如果文件是 104 行 长,它实际上会返回类似 的值>134 行其余 30 行全部为空??

已检查:已使用 UTF-8 格式,并再次检查文档本身内没有字面上的空行...

我认为 while 循环的编写方式不能将 line=null 值记录到内容列表?我在这里遗漏了什么吗?

感谢您提供任何建设性的信息!我很确定我在这里忽略了一些简单的事实......

【问题讨论】:

    标签: java android text inputstream bufferedreader


    【解决方案1】:

    你为什么不为你的信息创建 HTML 然后解析它。

    【讨论】:

    • 将它用于许多静态字节数组。除了解决当前的问题,不知道 HTML 在哪里可以获得更好的性能。然后我不得不重写大量代码来使用 HTML...不过感谢您的建议。
    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多