Reputation: 181
I have a .txt file
in my Assets
folder.
I tried using
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(getAssets().open("---.txt")));
I get an error underlining (getAssets().open("---.txt")));
Saying
OutputStreamWriter(java.io.OutputStream) in OutputStreamWriter cannot be applied to (java.io.InputStream)
I don't know how to write to this file, and I need help. It would also be good if I knew how to erase everything that's already in that file and write in a blank file...(Sorry for being unclear).
I know I can do this using PrintWriter
on PC, but I am learning Android
now.
Upvotes: 2
Views: 5624
Reputation: 3852
The assets folder is read-only, as well as its contents. If you wish to modify and save any modifications in an asset, consider storing a copy in the device storage, using Context.openFileOutput()
. Here's an example without exception handling:
// copy the asset to storage
InputStream assetIs = getAssets().open(filename);
OutputStream copyOs = openFileOutput(filename, MODE_PRIVATE);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = assetIs.read(buffer)) != -1) {
copyOs.write(buffer, 0, bytesRead);
}
assetIs.close();
copyOs.close();
// now you can open and modify the copy
copyOs = openFileOutput(filename, MODE_APPEND);
BufferedWriter writer =
new BufferedWriter(
new OutputStreamWriter(copyOs));
Upvotes: 2
Reputation: 194
You cannot write any files to assets or any raw dir.
Where will it be located in the Android file system. So write your txt file in internal OR external storage.
Upvotes: 2