[android]文件输入流和输出流实现文本内容的存储和提取

利用文件输入流和输出流来实现文本内容的存储和提取


关键代码

文件输出流

1.创建一个文件输入对象
FileOutputStream file = null;
2.存入数据

try {
	//打开一个文件
	file = openFileOutput("first", MODE_APPEND);
	//存入内容
	file.write(content.getBytes()); //要将数据传成byte类型保存
	//清理缓存
	file.flush();
	Toast.makeText(getApplicationContext(), "内容保存成功", 1).show();
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

说明:file = openFileOutput(“first”, MODE_APPEND);
openFileOutput()实例化有两参数 第一个是要打开的文件名,第二个使用文件的权限


文件输入流

//读取文件内的数据
try {
	FileInputStream readfile = openFileInput("first");
	
	//实例化 byte对象
	byte []butter = new byte[readfile.available()];
	//数据的读取
	readfile.read(butter);
	//关闭文件
	readfile.close();
	//将byte类型的数据进行转换
	String content = new String(butter);
	editText.setText(content);
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

效果图
1.文件的存储
[android]文件输入流和输出流实现文本内容的存储和提取_第1张图片
2.文件的提取
[android]文件输入流和输出流实现文本内容的存储和提取_第2张图片


下面是完整的java代码和布局代码

package com.example.save_in;

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

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bn_save = (Button)findViewById(R.id.save);
        Button bn_delete = (Button)findViewById(R.id.delete);
        Button bn_read = (Button)findViewById(R.id.read);
        
        final EditText editText = (EditText)findViewById(R.id.content);
        
        bn_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String  content = editText.getText().toString();

				//创建一个文件输入对象	
				FileOutputStream file = null;
				try {
					//打开一个文件
					file = openFileOutput("first", MODE_APPEND);
					//存入内容
					file.write(content.getBytes());
					//清理缓存
					file.flush();
					Toast.makeText(getApplicationContext(), "内容保存成功", 1).show();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        
        bn_read.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub//
				
				//读取文件内的数据
				try {
					FileInputStream readfile = openFileInput("first");
					
					//实例化 byte对象
					byte []butter = new byte[readfile.available()];
					//数据的读取
					readfile.read(butter);
					//关闭文件
					readfile.close();
					//将byte类型的数据进行转换
					String content = new String(butter);
					editText.setText(content);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
        bn_delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				editText.setText("");
			}
		});

         
    }

布局代码



    

        

你可能感兴趣的:(android)