1

我正在创建一个应用程序,我需要在其中读取文件,可能进行更改并回写。目前我在资产中有初始文件并且它可以正常读取,但是我刚刚发现您无法写入该位置。我想写入内部存储。

我应该在哪里写入文件,以便我的应用程序可以在手机上写入并访问文件?

我通过不同的实现不断收到各种不同的错误:以下为我提供了

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 方法

4

1 回答 1

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将从内部存储中返回您的文件

于 2013-04-02T00:22:21.190 回答