我正在尝试将数据从app数据库写入文本文件,我尝试了以下代码:
Context context = getApplicationContext();
File file = new File(context.getExternalFilesDir("routable"), "test.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (Exception ex) {
Log.e("open file Exception", ex.getMessage().toString());
}
}
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(file.getName(), Context.MODE_APPEND));
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
DAO dao = new DAO(getApplicationContext());
Cursor res = dao.getSalePoints();
res.moveToFirst();
while (!res.isAfterLast()) {
writer.write(res.getString(0) + ", ");
writer.write(res.getString(1) + ", ");
writer.write(res.getString(2) + ", ");
writer.write(res.getString(3) + ", ");
res.moveToNext();
}
writer.close();
outputStreamWriter.close();
} catch (Exception ex) {
Log.e("Writing file Exception", ex.getMessage().toString());
}
代码创建文件并且循环抛出数据,但不写入文件。
android.permission.WRITE_EXTERNAL_STORAGE"
在清单中。答案 0 :(得分:0)
context.openFileOutput(file.getName(),
替换为
new FileOutputStream(file,