[英]Editing a file using FileWriter
我正在嘗試編輯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 =新的FileWriter(file,true);
如果我在偶數行之間添加吐司,似乎會卡在
fw =新的FileWriter(fw);
正如@CommonsWare所說,您不能寫資源,但是可以使用openFileOutput和openFileInput以及BufferedReaders和BufferedWriters使用內部存儲。 你可以在這里檢查
您可以從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();
}
檢查並告知
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.