如何在Androids'/ data/data/pkg/files'目录中创建文件层次结构?

ale*_*2k8 8 android file

我尝试在Android的/ data/data/pkg/files目录中创建'foo/bar.txt'.

这似乎是文档中的矛盾:

要写入文件,请使用名称和路径调用Context.openFileOutput().

http://developer.android.com/guide/topics/data/data-storage.html#files

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

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int)

当我打电话的时候

this.openFileOutput("foo/bar.txt", Context.MODE_PRIVATE);
Run Code Online (Sandbox Code Playgroud)

抛出异常:

java.lang.IllegalArgumentException: File foo/bar.txt contains a path separator
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*Lew 13

看来你遇到了一个文档问题.如果深入研究ApplicationContext.java的源代码,事情看起来就不会更好.在openFileOutput()里面:

File f = makeFilename(getFilesDir(), name);
Run Code Online (Sandbox Code Playgroud)

getFilesDir()总是返回目录"files".而且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");
}
Run Code Online (Sandbox Code Playgroud)

因此,通过使用openFileOutput()您将无法控制包含目录; 它总是会出现在"files"目录中.

但是,没有什么可以阻止您使用File和FileUtils在包目录中自己创建文件.它只是意味着你会错过使用openFileOutput()给你的便利(比如自动设置权限).

  • 使用SDK的Java应用程序无法使用`FileUtils`.你把它添加到项目库中吗?你从哪里下载图书馆? (2认同)

use*_*197 9

您可以在私有目录中添加带路径的文件

String path = this.getApplicationContext().getFilesDir() + "/testDir/";
File file = new File(path);
file.mkdirs();
path += "testlab.txt";
OutputStream myOutput;
try {
myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
write(myOutput, new String("TEST").getBytes());
myOutput.flush();
myOutput.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨泰德!文件类重写 toString() 方法。因此,将其与字符串连接将自动调用 toString() (2认同)

Com*_*are 7

使用getFilesDir()得到一个File在你的包的根files/目录下.

  • @avalancha:嗯,您必须在指向该目录的“File”对象上使用“mkdir()”来创建“subfolder/”目录。 (3认同)

Dav*_*ile 5

要写入内部存储器子文件夹中的文件,首先需要创建文件(如果尚未存在,则需要创建子文件夹),然后创建FileOutputStream对象.

这是我使用的方法

    private void WriteToFileInSubfolder(Context context){
    String data = "12345";
    String subfolder = "sub";
    String filename = "file.txt";

    //Test if subfolder exists and if not create
    File folder = new File(context.getFilesDir() + File.separator + subfolder);
    if(!folder.exists()){
        folder.mkdir();
    }


    File file = new File(context.getFilesDir() + File.separator 
                         + subfolder + File.separator + filename);
    FileOutputStream outstream;

    try{
        if(!file.exists()){
            file.createNewFile();
        }

        //commented line throws an exception if filename contains a path separator
        //outstream = context.openFileOutput(filename, Context.MODE_PRIVATE);
        outstream = new FileOutputStream(file);
        outstream.write(data.getBytes());
        outstream.close();

    }catch(IOException e){
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)