2011-05-19 102 views
9

嗨我需要創建和寫入文本文件到/ data目錄中。創建和寫入Android設備/數據文件夾的文件

這是我正在使用的代碼。

try { 
    Runtime runtime = Runtime.getRuntime(); 
    Process process = runtime.exec("chmod 777 /data"); 
    process.waitFor(); 

    resultFile = new File(Environment.getDataDirectory() 
          + "/resultfile.txt"); 
    Log.i(TAG,"File Object Created....."+resultFile); 
} catch (Exception e) { 
    Log.i(TAG, "Exception in Environment.getDataDirectory()" 
       + e.toString()); 
} 

try { 
    try { 
     Runtime runtime = Runtime.getRuntime(); 
     Process process = runtime.exec("chmod 777 /data/resultfile.txt"); 
     process.waitFor(); 

     if (!resultFile.exists()) 
      resultFile.createNewFile(); 

    } catch (IOException ioException) { 
     Log.i(TAG, "Exception in creating file..." 
        + ioException.toString()); 
    } 

    try { 
     if (fileWriter == null) 
      fileWriter = new FileWriter(resultFile); 
    } catch (FileNotFoundException fileNotFoundException) { 
      Log.i(TAG, "FileNotFoundException....." 
         + fileNotFoundException.toString()); 
    } 

,但我得到下面這些問題......

Exception in creating file...java.io.IOException: Permission denied 

FileNotFoundException.....java.io.FileNotFoundException: /data/resultfile.txt (Permission denied) 

Exception in saveResultsToFile.....java.lang.NullPointerException 

PLZ幫我在這...謝謝。

回答

8

這將無法正常工作,因爲您沒有任何權限可在其中寫入。該目錄由root擁有。

+0

然後,我不想將其存儲在共享首選項或外部存儲器中的替代方法是什麼... – brig 2011-05-19 07:19:08

+0

您沒有任何其他選項。您可以將應用程序保存在應用程序的專用區域或外部存儲器中。 – 2011-05-19 07:21:41

+0

什麼意思是應用程序的私人領域,可以詳細說明這個.. ?? – brig 2011-05-19 07:28:31