我正在尝试编辑R.raw文件夹中的现有文件 我能够读取该文件 但是当我运行写入功能时,它无法正常工作。
public void tofile(View v){
BufferedWriter bw=null;
FileWriter fw =null;
try {
String path = ("/Page2/res/raw/text.txt");
File file = new File(path);
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write("hello");
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我甚至试过
fw = new FileWriter(file,true);
如果我在偶数行之间添加吐司,它似乎陷入了
fw = new FileWriter(fw);
答案 0 :(得分:0)
由于@CommonsWare表示你不能写资源,但你可以使用openFileOutput和openFileInput以及BufferedReaders和BufferedWriters来使用内部存储。你可以在这里查看
Write to a Text File Resource in Android
How to write a file in Android to the raw folder?
您可以从res / raw文件夹dude
中的文本文件中读取内容从res文件夹中读取文件
public String readStringFromResource(Context ctx, int resourceID) {
StringBuilder contents = new StringBuilder();
String sep = System.getProperty("line.separator");
try {
InputStream is = ctx.getResources().openRawResource(R.raw.trails);
BufferedReader input = new BufferedReader(new InputStreamReader(is), 1024*8);
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(sep);
}
}
finally {
input.close();
}
}
catch (FileNotFoundException ex) {
Log.e(TAG, "Couldn't find the file " + resourceID + " " + ex);
return null;
}
catch (IOException ex){
Log.e(TAG, "Error reading file " + resourceID + " " + ex);
return null;
}
return contents.toString();
}
检查并通知