在类中使用openFileOutput(). (不是活动) [英] using openFileOutput() in a class. (not an activity)

查看:103
本文介绍了在类中使用openFileOutput(). (不是活动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Activity类调用了另一个非活动类,当我尝试使用openFileOutput时,我的IDE告诉我openFileOutput是未定义的.请帮助:

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

推荐答案

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

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.

这篇关于在类中使用openFileOutput(). (不是活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆