从mkdir()创建文件失败 [英] Create file failed from mkdir()

查看:2198
本文介绍了从mkdir()创建文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Android模拟器中创建文件,但是当我完成代码时. 我找不到从android设备监视器创建的文件.

I try to create file in my android emulator, but when i finish my code. I can't find the file i create from android device monitor.

这是我的代码:

try {
    if (Environment.getExternalStorageState()
            .equals(Environment.MEDIA_MOUNTED)) {
        System.out.println("can be read and write");
        File sdFile = android.os.Environment.getExternalStorageDirectory();

        //String path = sdFile.getPath() + File.separator + "DestPdf";
        String path = sdFile.getPath() + "/demos/file/tmp/test";
        File dirFile = new File(path);

        if (!dirFile.exists()) { // if file doesn't exist
            System.out.println("create file");
            dirFile.mkdir(); // create file
            System.out.println(dirFile.toString());
        }
    }
} catch (Exception ex) {
    ex.toString();
}

我还添加了一些权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

这是我的打印输出,我可以看到根目录/storage/sdcard/demos/file/tmp/test

Here is my print out, i can see the root /storage/sdcard/demos/file/tmp/test

11-17 08:38:10.263 15501-15501/? I/System.out: can be read and write
11-17 08:38:10.263 15501-15501/? I/System.out: create file
11-17 08:38:10.264 15501-15501/? I/System.out: /storage/sdcard/demos/file/tmp/test

但是我无法从android设备监视器中找到文件

But i can't find the file from android device monitor

我错过了哪一步?任何帮助,将不胜感激 .预先感谢.

What step i miss it ? Any help would be appreciated . Thanks in advance.

推荐答案

您应该替换

dirFile.mkdir();

dirFile.mkdirs();

示例:

if (!dirFile.exists()) { // if file doesn't exist
        System.out.println(dirFile.mkdir());
        System.out.println(dirFile.mkdirs());

    }

对于第一个将生成false [并且将不创建目录],对于第二个将生成true,您将创建/demos/file/tmp/test

will yield false for the first [and no dir will be created], and true for the second, and you will have created /demos/file/tmp/test

mkdirs()还在此文件表示的路径中创建父目录.

mkdirs() also creates parent directories in the path this File represents.

用于mkdirs()的javadocs:

javadocs for mkdirs():

创建以此抽象路径名命名的目录,包括任何 必要但不存在的父目录.请注意,如果这 操作失败,它可能已经成功创建了一些 必要的父目录.

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

mkdir()的Javadocs:

javadocs for mkdir():

创建以此抽象路径名命名的目录.

Creates the directory named by this abstract pathname.

希望可以帮到您!

这篇关于从mkdir()创建文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆