How to Edit the Text Files in Assets Folder in Android

How can I edit the text files in assets folder in Android?

Writing into /assets directory at runtime? AFAIK that's not possible.

You can put the original file in /assets, and at the first application run copy it over to the /sdcard.

Adding a big text file to assets folder

Files over 1 MB placed in the assets folder won't be readable from your app (It'll throw an exception). This is because they get compressed during the build process, and thus the phone requires substantial resources to uncompress them when on the handset.

I believe you can place them in the raw folder, where they won't get compressed or use an extension that AAPT assumes is already compressed (see here)

However, It's not good having a 4.5 MB text file uncompressed sitting in the APK, It's wasted space that could be handled better. Try thinking about downloading the data on first use instead, or splitting the file into chunks as suggested before so that AAPT can compress it.

How to open and read a text file in my /assets directory?

What you might want to try is use:

AssetManager am = getAssets();
InputStream is;
is = am.open(pathToFile.txt);

Then use a byte[] buffer (get size using is.available(); ) and is.read(buffer)
Don't forget to is.close() after read.

If you're not in an Activity you have to use context.getAssets() in order to get the AssetManager.

Hope that helps you.

Where to put text files in directory in Android

Use assets or raw folder in your android folders structure to keep that file. For more info read this

How to read/write from .txt file in android

1) Place your txt file in assets.

2) At the first launch copy your txt file from assets to internal storage

Get InputStream of file in assets:

InputStream inputStream = am.open(inputFile);

Get OutputStream of file in internal storage:

File f = context.getFileStreamPath("filename.txt");
OutputStream outputStream = new FileOutputStream(f);

Copy data from input stream to output stream:

ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 8);

ReadableByteChannel ich = Channels.newChannel(inputStream);
WritableByteChannel och = Channels.newChannel(outputStream);

while (ich.read(buffer) > -1 || buffer.position() > 0)
{
buffer.flip();
och.write(buffer);
buffer.compact();
}
ich.close();
och.close();

3) Read data from internal storage:

File f = context.getFileStreamPath("filename.txt");
FileReader fr = new FileReader(f);
int chr = fr.read(); // read char
fr.close();

4) Write data to internal storage:

File f = context.getFileStreamPath("filename.txt");
FileWriter fw = new FileWriter(f);
fw.write("word"); // write string
fw.close();

You can use BufferedReader instead of FileReader for read file line by line.

Use a text file stored in ASSETS folder

AssetManger#open(String) will throw exception and you need handle it.

public final InputStream open(String fileName) throws IOException {
return open(fileName, ACCESS_STREAMING);
}

So you need:

   try {
InputSR = new InputStreamReader(am.open("test_scores.txt"));
BufferedRdr = new BufferedReader(InputSR);
// open input stream test_scores for reading purpose.
int i = 0;
while ((thisLine = BufferedRdr.readLine()) != null) {
// System.out.println(thisLine);

String[] parts = thisLine.split(" ");
testScoreList[i][0] = parts[0];
testScoreList[i][1] = parts[1];
i = i +1;
}
} catch (Exception e) {
e.printStackTrace();

}

Need to read from a txt file in the assets folder in android studio

Well I found the answer. Thank you for pointing me in the right direction.
Here it is

    try {
InputStream is = getAssets().open("round.txt");

// We guarantee that the available method returns the total
// size of the asset... of course, this does mean that a single
// asset can't be more than 2 gigs.
int size = is.available();

// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();

// Convert the buffer into a string.
String text = new String(buffer);

// Finally stick the string into the text view.
// Replace with whatever you need to have the text into.

TextView tv = (TextView)findViewById(R.id.text);
tv.setText(text);

} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}

get the names of txt files from assets folder into a list

it should be

String[] files = assetManager.list("");

if you want to get what's inside a subfolder pass the name of the subfolder:

String[] files = assetManager.list("mySubFolder");


Related Topics



Leave a reply



Submit