【问题标题】:using openFileOutput() in a class. (not an activity)在类中使用 openFileOutput()。 (不是活动)
【发布时间】:2011-05-03 22:04:32
【问题描述】:

我的 Activity 类调用另一个非 Activity 类,当我尝试使用 openFileOutput 时,我的 IDE 告诉我 openFileOutput 未定义。请帮忙:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;

import android.util.Log;
import android.content.Context;

public class testFile(){

Context fileContext;

public testFile(Context fileContext){
    this.fileContext = fileContext;
}

public void writeFile(){
    try{
            FileOutputStream os = fileContext.getApplicationContext().openFileOutput(fileLoc, Context.MODE_PRIVATE);
        os.write(inventoryHeap.getBytes()); // writes the bytes
        os.close();
        System.out.println("Created file\n");
    }catch(IOException e){
        System.out.print("Write Exception\n");
    }
}
}

【问题讨论】:

    标签: java android file


    【解决方案1】:

    你已经有了一个上下文。

    FileOutputStream os = fileContext.openFileOutput(fileLoc, Context.MODE_PRIVATE);
    

    【讨论】:

      【解决方案2】:

      我之前删除了我的答案,因为我错了,我看到的问题是你在类声明中添加了()public class testFile(){。它应该是public class testFile{。就是这样。

      【讨论】:

      • 感谢您的帮助。你之前的回答是正确的。我只是输入了错误的示例,这就是为什么我有 public class testFile(){
      • 你这么说很有趣,因为使用了你的确切例子,但只有我在这个答案中写的修复,我没有错误......
      • 好的……我就是这么做的。我让非活动课程扩展了我的活动课程。我在我的非活动类中取出了 getContextApplication() 。我还将 fileContext 的类型更改为 Activity。
      【解决方案3】:

      我写这篇文章是为我自己而不是为其他任何人。我是android编程的新手。我有同样的问题,我通过将上下文作为参数传递给方法来修复。在我的例子中,该类试图使用我在 Java 示例中找到的一段代码写入文件。由于我只是想写一个对象的持久化,不想关心文件在“哪里”,所以我修改如下:

      public static void Test(Context fileContext) {
        Employee e = new Employee();
        e.setName("Joe");
        e.setAddress("Main Street, Joeville");
        e.setTitle("Title.PROJECT_MANAGER");
        String filename = "employee.ser";
        FileOutputStream fileOut =  fileContext.openFileOutput(filename, Activity.MODE_PRIVATE); // instead of:=> new FileOutputStream(filename);
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(e);
        out.close();
        fileOut.close();
      }
      

      并从调用活动中我使用以下内容:

      SerializableEmployee.Test(this.getApplicationContext());
      

      工作就像一个魅力。然后我可以用(简化版)阅读它:

      public static String Test(Context fileContext) {
        Employee e = new Employee();
        String filename = "employee.ser";
        File f = new File(filename);
        if (f.isFile()) {
          FileInputStream fileIn = fileContext.openFileInput(filename);// instead of:=> new FileInputStream(filename);
          ObjectInputStream in = new ObjectInputStream(fileIn);
          e = (Employee) in.readObject();
          in.close();
          fileIn.close();
         }
        return e.toString();
      }
      

      【讨论】:

        【解决方案4】:

        您可以尝试将Context fileContext; 更改为static Context fileContext;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-20
          • 2015-04-14
          • 1970-01-01
          • 1970-01-01
          • 2015-04-05
          • 2011-12-01
          • 1970-01-01
          相关资源
          最近更新 更多