2012-03-09 43 views
3

我試圖在/data/data/package_name/files子目錄下創建文件。 例如,/data/data/package_name/files/folder1/file1.txt。這是我的代碼:android:在/ data/data子目錄下創建文件/ <package>/files

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

這對我有用。

相關問題