簡體   English   中英

在android設備的/ data文件夾中創建和寫入文件

[英]Creating and writing file in to /data folder of android device

嗨,我需要創建文本文件並將其寫入/ data目錄。

這是我正在使用的代碼。

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

請幫助我...謝謝。

這不起作用,因為您沒有任何權限可以在那里寫入。 該目錄由root擁有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM