Android檔案操作

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   java   

第一步:前台頁面main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/fileName" />    <EditText        android:id="@+id/txtName"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint=""        android:maxLines="1" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/fileContext" />    <EditText        android:id="@+id/txtContext"        android:layout_width="fill_parent"        android:layout_height="100dp"        android:gravity="left"        android:hint=""        android:minLines="4" />    <Button        android:id="@+id/btnSave"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/btnSave" /></LinearLayout>

第二步:後台代碼

package apk.example.androidapk;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.app.Activity;import apk.example.service.FileServer;public class MainActivity extends Activity {    public Button btnSave;    public EditText txtName, txtContext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        txtName = (EditText) findViewById(R.id.txtName);        txtContext = (EditText) findViewById(R.id.txtContext);        btnSave = (Button) findViewById(R.id.btnSave);        btnSave.setOnClickListener(new BtnSaveListener());    }    private final class BtnSaveListener implements View.OnClickListener {        @Override        public void onClick(View v) {            String fileName = txtName.getText().toString();            String fileContext = txtContext.getText().toString();            FileServer server = new FileServer(getApplicationContext());            try {                if (Environment.getExternalStorageState().equals(                        Environment.MEDIA_MOUNTED)) {                    server.saveToSDCard(fileName, fileContext);                    // 儲存成功                    Toast.makeText(getApplicationContext(), R.string.success,                            Toast.LENGTH_LONG).show();                } else {                    // SDCard不存在或者防寫保護                    Toast.makeText(getApplicationContext(),                            R.string.sdCardMessage, Toast.LENGTH_LONG).show();                }            } catch (Exception e) {                // 儲存失敗                Toast.makeText(getApplicationContext(), R.string.fail,                        Toast.LENGTH_LONG).show();                e.printStackTrace();            }        }    }}
package apk.example.service;import java.io.File;import java.io.FileOutputStream;import android.content.Context;import android.os.Environment;public class FileServer {    private Context context;    public FileServer(Context context) {        this.context = context;    }    /**     *      * @param 檔案名稱     * @param 檔案內容     * @throws 異常資訊     */    public void save(String fileName, String fileContext) throws Exception {        // 私人操作模式:建立出來的檔案只能被本應用訪問,其它應用無法訪問該檔案,另外採用私人操作模式建立的檔案,寫入檔案中的內容會覆蓋原檔案的內容        FileOutputStream outStream = context.openFileOutput(fileName,                Context.MODE_PRIVATE);        outStream.write(fileContext.getBytes());        outStream.close();    }    /**     *      * @param 檔案名稱     * @param 檔案內容     * @throws 異常資訊     */    public void saveToSDCard(String fileName, String context) throws Exception {        // 私人操作模式:建立出來的檔案只能被本應用訪問,其它應用無法訪問該檔案,另外採用私人操作模式建立的檔案,寫入檔案中的內容會覆蓋原檔案的內容        File file = new File(Environment.getExternalStorageDirectory(),                fileName);        FileOutputStream outStream = new FileOutputStream(file);        outStream.write(context.getBytes());        outStream.close();    }}

註:在AndroidManifest.xml檔案中添加下列許可權。

    <!-- 在SDCard中建立與刪除檔案許可權 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!-- 往SDCard寫入資料許可權 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

Android檔案操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.