安卓存储之文件存储方式j

支持往文件中读写数据,创建新的文件

Main

package com.chuanxidemo.shaoxin.demo06;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by shaoxin on 2017/2/23.
 */

public class Main extends AppCompatActivity {
    private Button read;
    private Button write;
    private FileOutputStream outputStream;

    private FileInputStream inputStream;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        read = (Button) findViewById(R.id.read);
        write = (Button) findViewById(R.id.write);
        read.setOnClickListener(new MyClick());
        write.setOnClickListener(new MyClick());

    }

    public class MyClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.write:
                    try {
                        outputStream = openFileOutput("test.txt", MODE_APPEND);
                        outputStream.write("123".getBytes());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (outputStream != null) {
                                outputStream.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case R.id.read:
                    try {
                        inputStream = openFileInput("test.txt");
                        byte[] bytes = new byte[1024];
                        int num = 0;
                        int i =0;
                        while ((num = inputStream.read())!=-1){
                           bytes[i]= (byte) num;
                            i++;
                        }
                        Log.i("msg", "onClick: "+new String(bytes,0,i));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if (inputStream!=null){
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
            }
        }
    }
}

main.xml

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

    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读文件" />

    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写文件" />
</LinearLayout>

 

posted @ 2017-02-23 09:58  情似雨餘黏地絮  阅读(213)  评论(0编辑  收藏  举报