Android: create files under subdirectory in / data / data / <package> / files

I am trying to create files under a subdirectory in /data/data/package_name/files

. Eg /data/data/package_name/files/folder1/file1.txt

. This is my code:

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

      

I don't know if I have permission. Will the pod files be saved /data/data/package_name/files

when the user restarts the application? I don't want the user to see my files, so writing to the SD card will not solve my problem.

+3


source to share


1 answer


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){}
}

      



This worked for me.

0


source







All Articles