安卓寫檔案及檔案夾

來源:互聯網
上載者:User

標籤: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();}


安卓寫檔案及檔案夾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.