Java 使用FileWriter编辑文件
我正在尝试编辑我的R.raw文件夹中的现有文件 我能读懂这个文件 但是当我运行write函数时,它不工作Java 使用FileWriter编辑文件,java,android,Java,Android,我正在尝试编辑我的R.raw文件夹中的现有文件 我能读懂这个文件 但是当我运行write函数时,它不工作 public void tofile(View v){ BufferedWriter bw=null; FileWriter fw =null; try { String path = ("/Page2/res/raw/text.txt"); File file = new File(p
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=新的FileWriter(文件,true)
若我在两行之间加上吐司,它似乎会被卡住
fw=新文件写入程序(fw) 你不会写字
正如@commonware所说的,您不能在资源上写东西,但可以使用内部存储,使用openFileOutput和openFileInput以及BufferedReaders和BufferedWriter。你可以在这里查一下
从以下链接中@rodkarom的答案
而@Andro Selva在下面的链接中也说了同样的话
您可以从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();
}
检查并通知字符串路径=(“/Page2/res/raw/text.txt”),这不是android上的有效路径。你能描述一下你正在好转的具体错误吗?你有对应于这些操作的logcat条目吗?你看到我的帖子了吗