首页 > 代码库 > 安卓写文件及文件夹

安卓写文件及文件夹

</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();
}


安卓写文件及文件夹