2012/08/20

Android寫入檔案

寫入檔案其實有分使用私人的權限去寫入檔案,以及普通的寫入方式
這次我所採用的是透過私人的方式去寫入檔案,你們一定很好奇這樣的好處以及壞處的分析

好處:在於其他程式沒辦法打開你寫入的檔案
壞處:要編輯的時候只能透過寫入的程式才能做讀取的動作,如果原先那個程式被移除了,那可能就得透過PC的OS來做強制刪除的動作



Activity程式碼如下:
package com.example.write;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button, button2;
    private final static String path = "/data/data/com.example.write/files/test.txt";
    private File file;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    void init() {
        button = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        file = new File(path);
        button.setText("Write");
        button2.setText("Read");
        button.setOnClickListener(str);
        button2.setOnClickListener(str);
    }

    void write() throws IOException {
        if (file.isFile()) {
            Log.i("Tag:", "檔案存在");
        } else {
            DataOutputStream ds = new DataOutputStream(this.openFileOutput(
                    "test.txt", MODE_PRIVATE));
            for (int i = 0; i < 5; i++) {
                ds.writeUTF("test");
            }
            ds.flush();
            ds.close();
        }
    }

    void load() throws IOException {
        DataInputStream ds = new DataInputStream(new FileInputStream(file));
        String str = ds.readLine();
        while (str != null) {
            Log.i("Tag", str);
            str = ds.readLine();
        }
        ds.close();
    }

    Button.OnClickListener str = new Button.OnClickListener() {
        public void onClick(View v) {
            try {
                switch (v.getId()) {
                case R.id.button1:
                    write();
                    break;
                case R.id.button2:
                    load();
                    break;
                }

            } catch (IOException e) {
                Log.e("IOError:", e.toString());
            }
        }
    };
}


AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.write"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

參考文章:
http://1622511.blog.51cto.com/1612511/576746