標籤:android 檔案 檔案夾
</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();}
安卓寫檔案及檔案夾