写入Android库中的文件

时间:2014-11-30 21:10:37

标签: java android eclipse

我正在编写Android库,并希望写入assets / myfile.txt。在进行一些阅读后,看起来资产只在运行时读取。所以我决定尝试通过openFileOutput写一个内部存储(" myfile.txt",Context.MODE_PRIVATE);

这里的问题是找不到openFileOutput()。我使用向导在Eclipse中创建了一个库。在我的项目中,我有以下接口和类。

public interface MyClass {
  public void writeToFile(String key, String value) throws IOException;
}

public class MyClassImpl {
  @Override
  public void writeToFile(String key, String value) throws IOException {
    OutputStreamWriter output = new OutputStreamWriter(openFileOutput("myfile.txt", Context.MODE_PRIVATE));
    // ...REST OF METHOD TO WRITE TO THIS FILE...
  }
}

我定位了最新的API,当我在Eclipse中编写上述代码时出现以下错误

  

对于类型,未定义openFileOutput(String,int)方法   ClientImpl

我是否需要在此处扩展一些类以在我的接口/类中实现openFileOutput()?

1 个答案:

答案 0 :(得分:1)

  

这里的问题是找不到openFileOutput()

openFileOutput() is a method on Context。例如,您的ActivityContext的子类。

  

我是否需要在此处扩展一些类以在我的接口/类中实现openFileOutput()?

您需要在openFileOutput()的有效实例上致电Context,例如Activity。例如,您可以将一个作为参数传递给writeToFile()方法。