首页 > 代码库 > 安卓写文件及文件夹
安卓写文件及文件夹
</pre><span style="font-size:18px">首先,在开始具体操作前,我们必须熟悉安卓File类的构造函数,见我的文章安卓File类</span><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center">http://blog.csdn.net/hemeng2009/article/details/40398063</span><p></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center">其次,明确需求,根据文件还是文件夹选择File类的构造函数。</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center">读写权限:</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center">在AndroidManifest文件中加入读写权限:</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center"></span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"><!-- 往sdcard中写入数据的权限 --></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" ></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"> </uses-permission></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"> <!-- 在sdcard中创建/删除文件的权限 --></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"> <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 13.63636302947998px; line-height: 25.99431800842285px;"> </uses-permission></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:16.363636016845703px; line-height:35.99431610107422px; text-align:center">文件夹:</span><span style="line-height:35.99431610107422px; color:rgb(51,51,51); font-family:'Microsoft YaHei'; font-size:18px; background-color:rgb(240,240,240); text-align:center"></span></p><p><pre name="code" class="java">if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)//判断是否有读写权限
{
<span> </span>File root = new File(Environment</span>.getExternalStorageDirectory().getPath() + "/sign/");//构造函数参数为路径
<span> </span>if (!root.exists())
<span> </span>{root.mkdirs();}<span style="font-family:Microsoft YaHei;color:#333333;"><span style="font-size: 12px; line-height: 35.99431610107422px;"> </span></span>
}
</pre><p></p><pre>
文件:
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/sign/",filename);//注意构造函数的选择 if (!file.exists()) { <span style="white-space:pre"> </span>file.createNewFile(); }
安卓写文件及文件夹
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。