问题
Does anyone know how to write to a text file in Android using a resource like:
R.raw.my_text_file
I'm just looking for something clean and simple. I've Googled around but the examples I found didn't work. I tried to code using the Android docs but couldn't wrap my head around it...
Thanks
EDIT:
I've used the Android docs to create this code. The logs print out "1" and "9" and the code skips everything in between and does nothing:
try {
String filename = "res/raw/my_text_file.txt";
String string;
Log.v(TAG, "1");
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
Log.v(TAG, "2");
for (int i = 0; i < list.size(); i++) {
Log.v(TAG, "3");
try {
Log.v(TAG, "4");
string = i + " - " + list.get(i);
fos.write(string.getBytes());
} catch (Exception e) {
Log.v(TAG, "5");
}
Log.v(TAG, "6");
}
Log.v(TAG, "7");
fos.close();
Log.v(TAG, "8");
} catch (Exception e) {
}
Log.v(TAG, "9");
回答1:
Does anyone know how to write to a text file in Android using a resource
Resources are read-only at runtime and cannot be written to.
回答2:
As @CommonsWare said you can't write on resources but you could use internal storage using openFileOutput and openFileInput and BufferedReaders and BufferedWriters. You can check it here:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
But here's a sample:
BufferedReader BR_intFile = new BufferedReader(new InputStreamReader(openFileInput("file")));
To read and a similar code to write.
来源:https://stackoverflow.com/questions/10457053/write-to-a-text-file-resource-in-android