java 在类中使用 openFileOutput()。(不是活动)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5876278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using openFileOutput() in a class. (not an activity)
提问by ddan
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:
我的 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");
}
}
}
采纳答案by MByD
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.
我删除了我之前的答案,因为我错了,我看到的问题是您添加()
到类声明中:public class testFile(){
. 应该是public class testFile{
。就这样。
回答by Brian Dupuis
You've already got a Context.
你已经有了一个上下文。
FileOutputStream os = fileContext.openFileOutput(fileLoc, Context.MODE_PRIVATE);
回答by Alex Alvarez
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:
我写这篇文章是为我自己而不是为其他任何人写的。我是 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();
}
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();
}
回答by eaavendano
You might try changing Context fileContext;
to static Context fileContext;
您可以尝试更改Context fileContext;
为static Context fileContext;