【问题标题】:android: create files under a subdirectory in /data/data/<package>/filesandroid:在 /data/data/<package>/files 的子目录下创建文件
【发布时间】:2012-03-09 11:26:17
【问题描述】:

我正在尝试在/data/data/package_name/files 的子目录下创建文件。 例如,/data/data/package_name/files/folder1/file1.txt。这是我的代码:

FileOutputStream fos;
String path = getFilesDir().toString() + "/" + folderName + "/" + String.valueOf(i+1) + ".txt";
try
{
    File f = new File(path);
    f.getParentFile().mkdirs();
    f.createNewFile();
    fos = new FileOutputStream(path, false);
    fos.write(array[i].getBytes());
    fos.close();
}
catch (Exception e)
{
    e.printStackTrace();
}

我不知道我是否有权限。当用户重新启动应用程序时,/data/data/package_name/files 下的文件是否仍然可用?我不想让用户看到我的文件,所以写入 sdcard 并不能解决我的问题。

【问题讨论】:

    标签: android file subdirectory


    【解决方案1】:
    FileOutputStream fos;
    File f = getFilesDir();
    try{
        String fileName = "test.txt";
        String filePath = f.getAbsolutePath()+File.separator+fileName;
        fos = new FileOutputStream(filePath);
        fos.write(array[i].getBytes());
    }
    catch (Exception e){
        e.printStackTrace();
    }finally{
        try{
            fos.close();
        }catch(IOException e1){}
    }
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2011-08-03
      相关资源
      最近更新 更多