首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据持久化openFileOutput和openFileInput

数据持久化openFileOutput和openFileInput
EN

Stack Overflow用户
提问于 2015-10-23 22:38:45
回答 1查看 126关注 0票数 1

我正在尝试让我的rss阅读器保存URL地址,这样当它重新打开时,它就会保存它。但是出于同样的原因,openFileOutput和openFileInput是红色的,它说它不能解析方法。

我们的教授让我们在网上看了一段视频,我写的时候和他的一样,他的也在工作。他的视频https://www.youtube.com/watch?v=RlIHJNCKpkw

代码语言:javascript
复制
 private void saveArticles(){

        try{
                                       //is in red cannot resolve method
            FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(urlAddress.getText().toString().getBytes());
            fos.close();
        }
        catch (Exception e){
            Log.d("exception", e.getMessage());

        }
    }

        private String getSavedArticles(){
            String contexts = "";

            try{
                                     //is in red cannot resolve method
                FileInputStream fin = openFileInput(FILENAME);
                int c ;

                while((c = fin.read()) != -1){
                    contexts = contexts + (char)c;
                }
                fin.close();
                return contexts;
            }

            catch(Exception e){
                Log.d("exception", e.getMessage());
            }

            return "";

        }
EN

回答 1

Stack Overflow用户

发布于 2015-10-24 00:29:22

openFileInput和openFileOutput是Context类的成员。Activity继承自Context,因此您可以在Activity类中使用这些方法。否则,您将需要将该活动(或上下文)传递给另一个类。

将您的活动实例传递给实现saveArticles的类。

代码语言:javascript
复制
private void saveArticles(Context context)
{
    ...
     FileInputStream fit = context.openFileInput(FILENAME);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33305071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档