How do I read an assets file as a string

1

How do I read as a string an Android text file that is inside the Assets folder?

    
asked by anonymous 17.09.2014 / 16:49

1 answer

1

getAssets().open() will return InputStream .

Read it using the Java standard input and output (I / O) API. As in the following example:

StringBuilder buf=new StringBuilder();
InputStream json=getAssets().open("book/contents.json");
BufferedReader in=
    new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;

while ((str=in.readLine()) != null) {
  buf.append(str);
}

in.close();
    
17.09.2014 / 16:49