繁体   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