Read File from Assets

read file from assets

Here is what I do in an activity for buffered reading extend/modify to match your needs

BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));

// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}

EDIT : My answer is perhaps useless if your question is on how to do it outside of an activity. If your question is simply how to read a file from asset then the answer is above.

UPDATE :

To open a file specifying the type simply add the type in the InputStreamReader call as follow.

BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt"), "UTF-8"));

// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}

EDIT

As @Stan says in the comment, the code I am giving is not summing up lines. mLine is replaced every pass. That's why I wrote //process line. I assume the file contains some sort of data (i.e a contact list) and each line should be processed separately.

In case you simply want to load the file without any kind of processing you will have to sum up mLine at each pass using StringBuilder() and appending each pass.

ANOTHER EDIT

According to the comment of @Vincent I added the finally block.

Also note that in Java 7 and upper you can use try-with-resources to use the AutoCloseable and Closeable features of recent Java.

CONTEXT

In a comment @LunarWatcher points out that getAssets() is a class in context. So, if you call it outside of an activity you need to refer to it and pass the context instance to the activity.

ContextInstance.getAssets();

This is explained in the answer of @Maneesh. So if this is useful to you upvote his answer because that's him who pointed that out.

How to read a text file from assets in Android Studio?

File file = null;
try {
FileInputStream is = new FileInputStream(file);

Actually you are not using FileInputStream anywhere. Just use this piece of code

  try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("wifi2.txt")));
String line;
Log.e("Reader Stuff",reader.readLine());
while ((line = reader.readLine()) != null) {
Log.e("code",line);
String[] RowData = line.split(",");
LatLng centerXY = new LatLng(Double.valueOf(RowData[1]), Double.valueOf(RowData[2]));
if (RowData.length == 4) {
mMap.addMarker(new MarkerOptions().position(centerXY).title(String.valueOf(RowData[0]) + String.valueOf(RowData[3])).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}

}

} catch (IOException ex) {
ex.printStackTrace();
}

Read a Text asset(text file from assets folder) as a String in Kotlin (Android)

I found this in a youtube video. Here is the link https://www.youtube.com/watch?v=o5pDghyRHmI

val file_name = "qjsonfile.json"
val json_string = application.assets.open(file_name).bufferedReader().use{
it.readText()
}

Saves the JSON or text to the string json_string.

Flutter - Read text file from assets

The folder name "assets" isn't magically added. Update your pubspec.yaml to include the full path to the asset.

flutter:
assets:
- assets/res/my_file.txt

Get a file from assets, read it and copy the contents in another file

Read file from asset like this

  try {
InputStream is = context.getAssets().open("file name");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
} catch (Exception ex) {
ex.printStackTrace();
}

Then create new file and write this InputStream to that file.

give read and write permission in manifest file .

write input stream to file like this ------

 try {
InputStream inputStream = null;
OutputStream output = null;
try {
inputStream = getContentResolver().openInputStream(data.getData());
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"picture_G.jpg");
output = new FileOutputStream(file);

byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;

while ((read = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();


} finally {
inputStream.close();
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}

How to read file from assets folder in react-native?

There is readFileAssets is a method in react-native-fs.

Place your file in android\app\src\main\assets.If there is no assets folder
then just create it.

import fs from "react-native-fs";

fs.readFileAssets("folder/file", "base64") // 'base64' for binary
.then(binary => {
// work with it
})
.catch(console.error)

Note: Path must be relative. If android\app\src\main\assets\folder\file then use folder\file



Related Topics



Leave a reply



Submit