简体   繁体   中英

using openFileOutput() in a class. (not an activity)

my Activity class calls to another non-activity class and when i try to use openFileOutput, my IDE tells me that openFileOutput is undefined. please help:

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");
    }
}
}

You've already got a Context.

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

You might try changing Context fileContext; to static Context fileContext;

I'm writing this more for me than for anybody else. I'm new to android programming. I had the same problem and I fixed by passing the context as a parameter to the method. In my case the class was trying to write to a file using a piece of code that I found in a Java example. Since I just wanted to write the persistence of an object and didn't wanted to concern myself with the "where" the file was, I modified to the following:

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();
}

and from the calling activity I use the following:

SerializableEmployee.Test(this.getApplicationContext());

Worked like a charm. Then I could read it with (a simplified version):

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();
}

I deleted my answer from before, since I was wrong, the problem I see is that you add () to the class declaration: public class testFile(){ . it should be public class testFile{ . That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM