写入文本文件中的"追加模式"在模拟器模式, [英] Writing to text file in "APPEND mode" in emulator-mode,

查看:94
本文介绍了写入文本文件中的"追加模式"在模拟器模式,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我应该从用户简单的文本文件存储数据,我在原目录中创建。在此之后,我试图用简单的code从谷歌的例子写在追加方式文件:

In my Android app I should store the data from user in simple text-file, that I created in the raw directory. After this, I'm trying to write file in APPEND MODE by using simple code from the Google's examples:

try 
{
    FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);
    fos.write((nameArticle+"|"+indexArticle).getBytes());
    fos.close();    
} 
catch (FileNotFoundException e) 
{               
    e.printStackTrace();
} 
catch (IOException e) 
{
    e.printStackTrace();
}

但没有任何反应。没有例外,但我可以看到什么我的 FILE_NAME ,除了单独的记录,这是由我添加

But nothing happens: no exceptions, but I can see nothing in my FILE_NAME, besides the single record, which was added by me.

我是什么做错了吗?是否有可能在共同谱写模拟器文件?

What am I doing wrong ? Is it possible at common to write to file in emulator ?

推荐答案

openFileOutput 将只允许你打开这个语境下的应用程序包写入相关的私人文件。我不知道该文件你要写信给所在。我的意思是完整路径。你可以用code下面写到位于任何地方的文件(只要你有烫发)。这个例子是使用外部存储,但你应该能够修改它的任何地方写:

openFileOutput will only allow you to open a private file associated with this Context's application package for writing. I'm not sure where the file you're trying to write to is located. I mean full path. You can use the code below to write to a file located anywhere (as long as you have perms). The example is using the external storage, but you should be able to modify it to write anywhere:

public Uri writeToExternalStoragePublic() {
    final String        filename        = mToolbar.GetTitle() + ".html"; 
    final String        packageName     = this.getPackageName();
    final String        folderpath      = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + packageName + "/files/";
    File                folder          = new File(folderpath);
    File                file            = null;
    FileOutputStream    fOut            = null;

    try {
        try {
            if (folder != null) {
                boolean exists = folder.exists();
                if (!exists) 
                    folder.mkdirs();                    
                file = new File(folder.toString(), filename);
                if (file != null) {
                    fOut = new FileOutputStream(file, false);
                    if (fOut != null) {
                        fOut.write(mCurrentReportHtml.getBytes());
                    }
                }
            }
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
        return Uri.fromFile(file);
    } finally {
        if (fOut != null) {
            try {
                fOut.flush();
                fOut.close();
            } catch (IOException e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

在你给的例子,试图抓住I0Exception`,我有一种感觉你没有,你试图写入权限。

In the example you have given, try catching 'I0Exception`, I have a feeling you do not have permission where you are trying to write.

有一个快乐的新年。

这篇关于写入文本文件中的"追加模式"在模拟器模式,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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