Android 數據的讀取與寫入

完成效果:在文本框中輸入數字或文字,點擊保存按鈕然後再按讀取按鈕,輸入內容顯示到按鈕下方。

                    在res下新建raw文件,點擊讀取raw按鈕,輸入內容顯示到按鈕下方。

                    新建一個assets,點擊讀取assets按鈕,輸入內容顯示到按鈕下方。

                    點擊刪除按鈕,刪除輸入內容。

佈局1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.jreduch09.InnerIOActivity">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="請輸入內容"
    android:id="@+id/content"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存"
        android:id="@+id/save"
        android:layout_below="@+id/content"
        android:layout_alignParentEnd="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="讀取"
        android:id="@+id/read"
        android:layout_below="@+id/content"
        android:layout_alignParentStart="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/read"
        android:id="@+id/show"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/delete"
        android:text="刪除"
        android:layout_below="@+id/content"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
代碼:

package com.example.administrator.jreduch09;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class SaveToSdCardActivity extends AppCompatActivity {
    private Button save,read,delete;
    private EditText content;
    private TextView show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_io);
         save= (Button) findViewById(R.id.save);
         read= (Button) findViewById(R.id.read);
         delete= (Button) findViewById(R.id.delete);
         show= (TextView) findViewById(R.id.show);
         content = (EditText) findViewById(R.id.content);
         save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            saveFile();
            }
        });
         read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               show.setText(readFile());
            }
        });
         delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFile();
            }
        });
    }
    //保存文件到SDcard中
    public void saveFile(){
        FileOutputStream fos=null;
        String state= Environment.getExternalStorageState();
        //獲取SD卡狀態
        if(!state.equals(Environment.MEDIA_MOUNTED)){
            //判斷SD卡是否就緒
            Toast.makeText(getBaseContext(),"請檢查SD卡",Toast.LENGTH_SHORT).show();
            return;
        }
         File file=Environment.getExternalStorageDirectory();
        //取得SD卡根目錄
        try {
            Log.d("===SD卡根目錄:",file.getCanonicalPath()+"sd.txt");
            fos=new FileOutputStream(file.getCanonicalPath()+"/sd.txt",true);
            String str=content.getText().toString();
            fos.write(str.getBytes());
            Toast.makeText(getBaseContext(),"保存成功",Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public String readFile(){
        BufferedReader reader =null;
        FileInputStream fis=null;
        //從SD卡讀取文件
        StringBuilder sdb=new StringBuilder();
        String state=Environment.getExternalStorageState();
        if(!state.equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(getBaseContext(),"SD卡未就緒",Toast.LENGTH_SHORT).show();
            return "";
        }
        File root=Environment.getExternalStorageDirectory();
        try {
            fis=new FileInputStream(root+"/sd.txt");
            reader=new BufferedReader(new InputStreamReader(fis));
            String row="";
            while ((row=reader.readLine())!=null){
                sdb.append(row);
            }
        } catch (FileNotFoundException e) {
            Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sdb.toString();
    }
    public void removeFile(){
        String state=Environment.getExternalStorageState();
        if(!state.equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(getBaseContext(),"SD卡未就緒",Toast.LENGTH_SHORT).show();
            return;
        }
        File file=Environment.getExternalStorageDirectory();
        File myFile=new File(file,"sd.txt");
        if(myFile.exists()){
            myFile.delete();
            Toast.makeText(this,"文件已刪除",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
        }
    }
}
效果展示:


佈局2:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.jreduch09.ReadRawAndAssetsActivity">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/raw"
    android:text="讀取raw"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/assets"
        android:text="讀取assets"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/show"
        android:layout_below="@+id/assets"
        />
</RelativeLayout>
代碼:
package com.example.administrator.jreduch09;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadRawAndAssetsActivity extends AppCompatActivity {
    private TextView show;
    private Button raw,assets;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_raw_and_assets);
        raw= (Button) findViewById(R.id.raw);
        assets= (Button) findViewById(R.id.assets);
        show= (TextView) findViewById(R.id.show);
        raw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               show.setText(readRaw());
            }
        });
        assets.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.setText(readAssets());
            }
        });
    }
    public String readRaw(){
        StringBuilder sbd=new StringBuilder();
        InputStream is=null;
        BufferedReader reader=null;
        is=getResources().openRawResource(R.raw.settings);
        String row="";
        try {
            reader=new BufferedReader(new InputStreamReader(is));
            while ((row=reader.readLine())!=null) {
                sbd.append(row);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sbd.toString();
    }
    public String readAssets(){
        StringBuilder sdb=new StringBuilder();
        BufferedReader reader=null;
        try {
            InputStream is=getResources().getAssets().open("city");
            reader=new BufferedReader(new InputStreamReader(is));
            String row="";
            while ((row=reader.readLine())!=null){
                sdb.append(row);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
        return sdb.toString();
    }
}
settings中的內容:

1.大家好我是VAE2.那是一條神奇的天路啊啊啊。
3.我願意爲你,我願意爲你。
4.如果你對天空嚮往。
assets中的city的內容:

北京上海廣州

效果展示:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章