using openFileOutput() in a class. (not an activity)
18,806
Solution 1
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.
Solution 2
You've already got a Context.
FileOutputStream os = fileContext.openFileOutput(fileLoc, Context.MODE_PRIVATE);
Comments
-
ddan almost 2 years
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"); } } }