如何在机器人“/数据/数据​​/包装/ files”目录下创建文件层次? [英] How to create files hierarchy in Androids '/data/data/pkg/files' directory?

查看:98
本文介绍了如何在机器人“/数据/数据​​/包装/ files”目录下创建文件层次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Android的/数据/数据​​/包装/文件目录下创建富/跳回到bar.txt。

I try to create 'foo/bar.txt' in Android's /data/data/pkg/files directory.

这似乎是在文档一个矛盾:

It seems to be a contradiction in docs:

要写入一个文件,调用Context.openFileOutput()的名称和路径。

To write to a file, call Context.openFileOutput() with the name and path.

http://developer.android.com/guide/主题/数据/数据​​storage.html#文件

要打开的文件的名称;不能包含路径分隔符。

The name of the file to open; can not contain path separators.

<一个href="http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String,%20int%29" rel="nofollow">http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int)

当我打电话

this.openFileOutput("foo/bar.txt", Context.MODE_PRIVATE);

抛出异常:

java.lang.IllegalArgumentException: File foo/bar.txt contains a path separator

那么,如何在子文件夹中创建文件?

So how do I create file in subfolder?

推荐答案

它似乎你已经遇到文档问题。事不要找任何更好,如果你深入到源$ C ​​$下ApplicationContext.java。里面的openFileOutput():

It does appear you've come across a documentation issue. Things don't look any better if you dig into the source code for ApplicationContext.java. Inside of openFileOutput():

File f = makeFilename(getFilesDir(), name);

getFilesDir()总是返回目录文件。而 makeFilename()

getFilesDir() always returns the directory "files". And makeFilename()?

private File makeFilename(File base, String name) {
    if (name.indexOf(File.separatorChar) < 0) {
        return new File(base, name);
    }
    throw new IllegalArgumentException(
        "File " + name + " contains a path separator");
}

因此​​,通过使用 openFileOutput(),你将无法控制含目录;它会永远结束在文件目录。

So by using openFileOutput() you won't be able to control the containing directory; it'll always end up in the "files" directory.

有,但是,没有从你的包目录中创建您自己的文件,使用文件和文件实用程序阻止你。它只是意味着你将错过,使用便捷 openFileOutput()为您提供(如自动设置权限)。

There is, however, nothing stopping you from creating files on your own in your package directory, using File and FileUtils. It just means you'll miss out on the conveniences that using openFileOutput() gives you (such as automatically setting permissions).

这篇关于如何在机器人“/数据/数据​​/包装/ files”目录下创建文件层次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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