Java 中 创建文件夹操作

来源:互联网 发布:天道总司 知乎 编辑:程序博客网 时间:2024/06/05 05:51

Section1 mkdir

if (Environment.MEDIA_MOUNTED.equals(        Environment.getExternalStorageState())){    String  path = Environment.getExternalStorageDirectory().getAbsolutePath();    path=path+File.separator+"1ATesthaha"+File.separator+"tempImage.png";    Toast.makeText(this,path,Toast.LENGTH_LONG).show();    File file = new File(path);    file.mkdir();}

检查文件发现没有创建成功


Section2 mkdirs

我们做一修改

if (Environment.MEDIA_MOUNTED.equals(        Environment.getExternalStorageState())){    String  path = Environment.getExternalStorageDirectory().getAbsolutePath();    path=path+File.separator+"1ATesthahab"+File.separator+"tempImage.png";    Toast.makeText(this,path,Toast.LENGTH_LONG).show();    File file = new File(path);    file.mkdirs();}
这次创建成功了,但是结果不是我们想要的
tempImage.png成了文件夹了
Section3  getParentFile
if (Environment.MEDIA_MOUNTED.equals(        Environment.getExternalStorageState())){    String  path = Environment.getExternalStorageDirectory().getAbsolutePath();    path=path+File.separator+"1ATesthahac"+File.separator+"tempImage.png";    Toast.makeText(this,path,Toast.LENGTH_LONG).show();    File file = new File(path);    file.getParentFile().mkdirs();}

这回结果符合预期。



原创粉丝点击