How I read an Assets file as a string

Asked

Viewed 246 times

1

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

1 answer

1

getAssets().open() will return a InputStream.

Read it using the standard Java input and output API (I/O). 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.