1. 程式人生 > >Android-Java讀寫檔案到自身APP目錄

Android-Java讀寫檔案到自身APP目錄

介面:

 

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et_output"
android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請寫入資料到檔案" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"> <
TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示讀取處理到資訊:" /> <TextView android:id="@+id/tv_input" android:layout_width="0dip" android:layout_height="wrap_content"
android:layout_weight="1" android:textColor="@android:color/black" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp"> <Button android:id="@+id/bt_output" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="寫入" /> <Button android:id="@+id/bt_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取" android:layout_alignParentRight="true" /> </RelativeLayout> </LinearLayout>

 

MainActivity讀寫相關程式碼:

package liudeli.datastorage;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity3 extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        initViewListener();
    }

    private EditText etOutpu;
    private TextView tvInput;
    public Button btOutput, btInput;

    private void initViewListener() {
        etOutpu = findViewById(R.id.et_output);
        tvInput = findViewById(R.id.tv_input);

        btOutput = findViewById(R.id.bt_output);
        btInput = findViewById(R.id.bt_input);

        btOutput.setOnClickListener(this);
        btInput.setOnClickListener(this);

        // 讓TextView獲得焦點,TextView就可以滾動了
        tvInput.setSelected(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_input: {
                // Java傳統方式 注意:訪問自身APP到目錄是不需要許可權 Linux目錄是以/為開頭
                File file = new File("/data/data/" + getPackageName() + "/my_file.txt");
                if (!file.exists()) {
                    Toast.makeText(MainActivity3.this, "檔案不存在", Toast.LENGTH_LONG).show();
                    return;
                }
                try {
                    // 使用字元讀取流
                    FileReader fileReader = new FileReader(file);
                    // 為什麼要用BufferedReader,因為BufferedReader有readLine()的方法
                    BufferedReader br = new BufferedReader(fileReader);
                    String result = br.readLine();
                    tvInput.setText(result + "");

                    fileReader.close();
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case R.id.bt_output: {
                String outputStr = etOutpu.getText().toString();
                if (TextUtils.isEmpty(outputStr)) {
                    Toast.makeText(MainActivity3.this, "請輸入內容!", Toast.LENGTH_SHORT).show();
                    return;
                }

                // Java傳統方式 注意:訪問自身APP到目錄是不需要許可權 Linux目錄是以/為開頭
                File file = new File("/data/data/" + getPackageName() + "/my_file.txt");
                try {
                    // 字元寫入流
                    FileWriter fileWriter = new FileWriter(file);
                    fileWriter.write(outputStr);
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
            default:
                break;
        }
    }
}

 

儲存的目錄: