开发者

Android/Java text file read problem [duplicate]

开发者 https://www.devze.com 2023-04-06 15:35 出处:网络
This question already has answers here: Android read text raw resource file (14 answers) Closed 3 years ago.
This question already has answers here: Android read text raw resource file (14 answers) Closed 3 years ago.

I'm currently trying to read a file from (res/raw) by using an InputStream that I dimension like such:

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

I then put tha开发者_开发知识库t into this method to return the values:

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;

}

My problem: It reads all the values as it should, but say if the file is 104 lines long, it will actually return a value of something like 134 total lines with the remaining 30 lines being full of null??

Have checked: Already using UTF-8 format, and double checked that there are literally no blank lines within the document itself...

I thought the way the while loop was written that it couldn't record a line=null value to contents List? Am I missing something here?

Thanks for any constructive information! I'm pretty sure I'm overlooking some simple factoid here though...


Why dont you create HTML for your information and then parse it.

0

精彩评论

暂无评论...
验证码 换一张
取 消