【问题标题】:android what is wrong with openFileOutput?android openFileOutput有什么问题?
【发布时间】:2010-09-02 09:38:15
【问题描述】:

我正在尝试使用 openFileOutput 函数,但它无法编译并且无法识别该函数。我正在使用安卓 SDK 1.6。这是一个sdk问题吗?这是参数问题吗?

import java.io.FileOutputStream;
public static void save(String filename, MyObjectClassArray[] theObjectAr) {
    FileOutputStream fos;
    try {
        fos = openFileOutput(filename, Context.MODE_PRIVATE);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(theObjectAr); 
        oos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    你的方法应该如下。接受一个额外的上下文作为参数。您可以将您的服务或活动传递给此方法

    public static void save(String filename, MyObjectClassArray[] theObjectAr, 
      Context ctx) {
            FileOutputStream fos;
            try {
                fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
    
    
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(theObjectAr); 
                oos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    

    【讨论】:

    • 我无法让它工作。我在这里为 ctx 尝试了传递的上下文 context.getApplicationContext()Application.instance()Application.instance().getApplicationContext(),它总是抛出 FileNotFoundException。
    • 原来,我遇到了权限问题。绝对不明显,“权限被拒绝”问题导致FileNotFoundException
    • 在哪里检查文件...哪个路径..?
    • Context.MODE_PRIVATE 使文件仅对您的应用可见,以防万一有人想知道。
    • @chinna_82 在developer.android.com/reference/java/io/File#getPath()中有一个getpath()方法
    【解决方案2】:

    您正在尝试从静态上下文调用非静态方法(您的方法具有静态修饰符)。您要么必须将方法设为非静态方法,要么传入 Context 实例(大多数情况下为活动实例)并调用对象上的方法。

    【讨论】:

    • 谢谢我也发现了这个问题。 :)
    【解决方案3】:

    您也不能在路径上openOutputStream。它会导致此异常:

    java.lang.IllegalArgumentException: File /storage/sdcard0/path/to/file.txt contains a path separator
    

    要解决这个问题,您需要创建一个文件对象,然后像这样创建它:

    String filename = "/sdcard/path/to/file.txt";
    File sdCard = Environment.getExternalStorageDirectory();
    filename = filename.replace("/sdcard", sdCard.getAbsolutePath());
    File tempFile = new File(filename);
    try
    {
        FileOutputStream fOut = new FileOutputStream(tempFile);
        // fOut.write();
        // fOut.getChannel();
        // etc...
        fOut.close();
    }catch (Exception e)
    {
        Log.w(TAG, "FileOutputStream exception: - " + e.toString());
    }
    

    【讨论】:

      【解决方案4】:

      你可以在静态类中使用 openFileOutput,如果你传递 View 如下:

       public static void save(View v, String fileName , String message){
      
          FileOutputStream fos = null;
      
          try {
      
              fos = v.getContext().openFileOutput(fileName, Context.MODE_PRIVATE);
              fos.write(message.getBytes());
      
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多