【发布时间】: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