文件存储

xml

<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入姓名:"/>
<EditText
    android:id="@+id/age"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入年龄:"/>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入"/>

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

</LinearLayout>

Java实现

package cn.edu.niit.store;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
//获取相关控件
private EditText username;
private EditText userage;
private Button write;
private Button read;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取组件
    username = (EditText) findViewById(R.id.name);
    userage = (EditText) findViewById(R.id.age);
    write= (Button)findViewById(R.id.write);
    //设置监听事件
    write.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = username.getText().toString();
            String age = userage.getText().toString();
            //使用saveToFile方法,对其进行写入
            if(saveToFile(name)){
                Toast.makeText( MainActivity.this,"保存成功" ,Toast.LENGTH_SHORT).show();

            }
        }
    });
    //设置监听事件
    read = (Button)findViewById(R.id.read);
    read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           String name = username.getText().toString();
            String filename = "data.txt";
            String result = "";
            try{
                //打开文件,并读取其数据
                FileInputStream in = openFileInput( filename );
                BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
                name = reader.readLine();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            name = username.getText().toString();
            String age = userage.getText().toString();
            Toast.makeText(MainActivity.this, "输入姓名"+name+",年龄:" + age,Toast.LENGTH_SHORT).show();
        }
    });
}

private boolean saveToFile(String Name) {
    //打开文件
    try {

        FileOutputStream out = openFileOutput("data.txt",  MODE_PRIVATE );
        BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
        writer.write(Name);
        out.close();//关闭文件输入流
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

}

效果图


posted @ 2017-05-09 13:37  阿狸飞  阅读(113)  评论(0编辑  收藏  举报