How to create files hierarchy in Androids '/da

2020-07-07 11:33发布

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

It seems to be a contradiction in docs:

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

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

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

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

And when I call

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

exception is thrown:

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

So how do I create file in subfolder?

标签: android file
5条回答
smile是对你的礼貌
2楼-- · 2020-07-07 12:14

Use getFilesDir() to get a File at the root of your package's files/ directory.

查看更多
叛逆
3楼-- · 2020-07-07 12:22

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

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

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).

查看更多
The star\"
4楼-- · 2020-07-07 12:24

To write to a file in a subfolder of internal storage you will need to create the file (and subfolder if not already there) first, then create the FileOutputStream object.

Here is the method I used

    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();
    }
}
查看更多
你好瞎i
5楼-- · 2020-07-07 12:26

Assuming the original post sought how to both create a subdirectory in the files area and write a file in it, this might be new in the docs:

public abstract File getDir (String name, int mode)

Since: API Level 1

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

查看更多
淡お忘
6楼-- · 2020-07-07 12:27

You can add files with path in private directory like that

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();
}
查看更多
登录 后发表回答