Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Assets file as string

Tags:

string

android

I would like to read the content of a file located in the Assets as a String. For example, a text document located in src/main/assets/

Original Question
I found that this question is mostly used as a 'FAQ' for reading an assets file, therefore I summarized the question above. Below is my original question

I'm trying to read a assets file as string. I have a file in my assets folder: data.opml, and want to read it as a string.

Some things I tried:

 AssetFileDescriptor descriptor = getAssets().openFd("data.opml");  FileReader reader = new FileReader(descriptor.getFileDescriptor()); 

And also:

 InputStream input = getAssets().open("data.opml");  Reader reader = new InputStreamReader(input, "UTF-8"); 

But without success, so a full example would be appreciated.

like image 393
Mdlc Avatar asked Apr 19 '13 17:04

Mdlc


People also ask

How do you read an asset file?

Right click on the assets folder, select New >> file (myText. txt) and your text.

How do I get an asset file on Android?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.


2 Answers

getAssets().open() will return an InputStream. Read from that using standard Java I/O:

Java:

StringBuilder sb = new StringBuilder(); InputStream is = getAssets().open("book/contents.json"); BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8 )); String str; while ((str = br.readLine()) != null) {     sb.append(str); } br.close(); 

Kotlin:

val str = assets.open("book/contents.json").bufferedReader().use { it.readText() } 
like image 60
CommonsWare Avatar answered Oct 14 '22 14:10

CommonsWare


There is a little bug CommonsWare's code - newline characters are discarded and not added to the string. Here is some fixed code ready for copy+paste:

private String loadAssetTextAsString(Context context, String name) {         BufferedReader in = null;         try {             StringBuilder buf = new StringBuilder();             InputStream is = context.getAssets().open(name);             in = new BufferedReader(new InputStreamReader(is));              String str;             boolean isFirst = true;             while ( (str = in.readLine()) != null ) {                 if (isFirst)                     isFirst = false;                 else                     buf.append('\n');                 buf.append(str);             }             return buf.toString();         } catch (IOException e) {             Log.e(TAG, "Error opening asset " + name);         } finally {             if (in != null) {                 try {                     in.close();                 } catch (IOException e) {                     Log.e(TAG, "Error closing asset " + name);                 }             }         }          return null;     } 
like image 34
Michael Litvin Avatar answered Oct 14 '22 13:10

Michael Litvin