我正在创建一个应用程序,我需要在文件中读取,可能会进行更改并回写。目前我有资产中的初始文件并且它读得很好,但我刚刚发现你无法写入该位置。我想写信给内部存储空间。
我应该在哪里写一个文件,以便我的应用可以在手机上写入并访问该文件?
我一直在使用不同的实现获得各种不同的错误:以下为我提供了
filenotfound异常:EROFS只读文件系统
public void write(Entry[] newEntries){
try {
FileOutputStream os = new FileOutputStream(new File("combinationswrite.txt"));
OutputStreamWriter osw = new OutputStreamWriter(os);
for (int j = 0; j < newEntries.length; j++){
if (newEntries[j] != null){
osw.write(newEntries[j].symbol);
osw.write("\t");
for (int i =0; i<6; i++){
osw.write(newEntries[j].getNode(i));
if( i < 5)
osw.write(",");
else
osw.write("\n");
}
}
}
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在文档中尝试以下实现时: http://developer.android.com/training/basics/data-storage/files.html
无法识别openFileOutput方法
答案 0 :(得分:0)
从您引用的链接中提取您的代码
public void write(Entry[] newEntries){
try {
FileOutputStream os = new FileOutputStream( getTempFile(context , "combinationswrite.txt"));
OutputStreamWriter osw = new OutputStreamWriter(os);
for (int j = 0; j < newEntries.length; j++){
if (newEntries[j] != null){
osw.write(newEntries[j].symbol);
osw.write("\t");
for (int i =0; i<6; i++){
osw.write(newEntries[j].getNode(i));
if( i < 5)
osw.write(",");
else
osw.write("\n");
}
}
}
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
catch (IOException e) {
// Error while creating file
}
return file;
}
此方法getTempFile
将从intarnal storage