我正在尝试在/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
下的文件是否仍然可用?我不希望用户看到我的文件,因此写入SD卡无法解决我的问题。
答案 0 :(得分:0)
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){}
}
这对我有用。