Android read text raw resource file
What if you use a character-based BufferedReader instead of byte-based InputStream?
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();
while (line != null) {
...
line = reader.readLine();
}
Don't forget that readLine()
skips the new-lines!
Reading from a raw resource text file
Think you are looking for something along the lines of
InputStream is = ctx.getResources().openRawResource(res_id);
Where ctx is a instance of Context
How to read file from res/raw by name
With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName());
To get it as a InputStream
InputStream ins = getResources().openRawResource(
getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION",
"raw", getPackageName()));
Reading JSON or text file in resource folder
If you want to read a file from res/raw
folder you can obtain InputStream
by R.raw.yourfile
id like this:
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/
folder and get InputStream
like this:
assets.open("yourfile.txt")
Write in a txt file located in /raw folder
I have a txt file in the /raw folder and I want to write into it
That is not possible at runtime. Resources and assets are read-only at runtime.
But gives me error in openRawResource()... How do i solve this?
openRawResource()
is for reading in the resource, and it gives you an InputStream
. You are welcome to write your data to an ordinary file, such as on internal storage.
Is it possible to read a raw text file without Context reference in an Android library project
Check out my answer here to see how to read file from POJO.
Generally, the res folder should be automatically added into project build path by ADT plugin. suppose you have a test.txt stored under res/raw folder, to read it without android.content.Context:
String file = "raw/test.txt"; // res/raw/test.txt also work.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
I did this before with an old SDK version, it should work with latest SDK as well. Give it a try and see if this helps.
write text file in res/raw folder
Resources contained in your raw directory in your project will be packaged inside your APK and will not be writeable at runtime.
Look at Internal or External Data Storage APIs to read write files.
https://developer.android.com/training/basics/data-storage/files.html
Related Topics
Animate the Transition Between Fragments
Percentage Width in a Relativelayout
Set Up Device For Development (? No Permissions)
How to Customize a Progress Bar in Android
How to Force Use of Overflow Menu on Devices With Menu Button
Android - Android.View.Inflateexception: Binary Xml File Line #8: Error Inflating Class Fragment
How to Send String from One Activity to Another
Set Icon For Android Application
How to Display a List View in an Android Alert Dialog
How to Get the Width and Height of an Android.Widget.Imageview
How to Detect When an Android Application Is Running in the Emulator
Android: How to Check If a View Inside of Scrollview Is Visible
How to Add Manifest Permission to an Application
Clicking Urls Opens Default Browser
How to Lay Out Views in Relativelayout Programmatically
Context.Startforegroundservice() Did Not Then Call Service.Startforeground()