【问题标题】:How to create files hierarchy in Androids '/data/data/pkg/files' directory?如何在 Android 的“/data/data/pkg/files”目录中创建文件层次结构?
【发布时间】:2009-12-11 16:26:14
【问题描述】:

我尝试在 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);

抛出异常:

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

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

【问题讨论】:

    标签: android file


    【解决方案1】:

    您确实遇到了文档问题。如果您深入研究 ApplicationContext.java 的源代码,事情看起来并没有好转。 openFileOutput() 内部:

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

    getFilesDir() 总是返回目录“文件”。还有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(),您将无法控制包含目录;它总是在“文件”目录中结束。

    但是,没有什么能阻止您使用 File 和 FileUtils 在包目录中自己创建文件。这只是意味着您将错过使用openFileOutput() 为您带来的便利(例如自动设置权限)。

    【讨论】:

    • FileUtils 不适用于使用 SDK 的 Java 应用程序。您是否将其添加到您的项目库中?你从哪里下载库?
    【解决方案2】:

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

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

    【讨论】:

    • getFilesDir() 返回一个文件,所以用字符串连接它是行不通的。
    • 嗨,泰德!文件类覆盖 toString() 方法。所以用字符串连接它会自动调用 toString()
    【解决方案3】:

    使用getFilesDir() 在包的files/ 目录的根目录中获取File

    【讨论】:

    • 我试过了,但是带有该参数的 FileOutputStream 失败了:/data/data/com.example.myapp/files/subfolder/foo.txt: open failed: EACCES (Permission denied)。还有什么我需要考虑的吗?
    • @avalancha:嗯,你必须创建subfolder/ 目录,在指向那里的File 对象上使用mkdir()
    【解决方案4】:

    要写入内部存储子文件夹中的文件,您需要先创建文件(如果还没有子文件夹,则创建子文件夹),然后创建 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();
        }
    }
    

    【讨论】:

      【解决方案5】:

      假设原始帖子寻求如何在文件区域中创建子目录并在其中写入文件,这可能是文档中的新内容:

      public abstract File getDir (String name, int mode)
      

      自:API 级别 1

      如果需要,检索并创建应用程序所在的新目录 可以放置自己的自定义数据文件。您可以使用返回的文件 对象来创建和访问此目录中的文件。请注意,文件 通过 File 对象创建的只能由您自己访问 应用;只能设置整个目录的模式,不能设置 单个文件。

      【讨论】:

        猜你喜欢
        • 2011-07-26
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-04
        • 1970-01-01
        相关资源
        最近更新 更多