数据存储之内部存储

package com.example.internalstorage;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private EditText editText;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) findViewById(R.id.editText_data);
		textView = (TextView) findViewById(R.id.textView_result);
	}

	/**
	 * 将editext 内容写到内部存储
	 * 
	 * @param v
	 */
	public void clickWrite(View v) {
		// 创建内部存储输入流对象
		OutputStream outputStream = null;
		try {
			outputStream = openFileOutput("info.txt", Context.MODE_PRIVATE);
			outputStream.write(editText.getText().toString().trim().getBytes());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭流对象
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 读取内部存储数据
	 * 
	 * @param view
	 */
	public void clickRead(View view) {
		BufferedReader bufferedReader = null;
		StringBuffer stringBuffer = new StringBuffer();
		try {
			InputStream inputStream = openFileInput("info.txt");
			// 将读取字节流转换成字符缓冲流
			bufferedReader = new BufferedReader(new InputStreamReader(
					inputStream));
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line);
			}
			textView.setText(stringBuffer.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭字节缓冲流
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 内部存储的写入
	 * 
	 * @param view
	 */
	public void clickwrite2(View view) {
		// 根据内部存储的路径创建file对象 getFilesDir() 获取内部存储的路径
		File file = new File(getFilesDir(), "test.txt");
		FileOutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(file);
			outputStream.write(editText.getText().toString().trim().getBytes());
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 读取内部存储的内容
	 * @param view
	 */
	public void clickread2(View view){
		File file=new File(getFilesDir(), "test.txt");
		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		FileInputStream inputStream=null;
		try {
			inputStream=new FileInputStream(file);
			int temp=0;
			byte[] buff=new byte[1024];
			while((temp=inputStream.read(buff))!=-1){
				outputStream.write(buff, 0,temp);
				outputStream.flush();
			}
			textView.setText(outputStream.toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	/**
	 * 写入内部存储的缓存文件
	 * @param view
	 */
	public void writeCache(View view){
		FileOutputStream outputStream=null;
		try {
			//创建内部存储的缓存文件对象   1参数表示文件名称 2参数表示文件名称的后缀 如果为null .tmp 3参数表示内部缓存文件的路径
			File file=File.createTempFile("test",null, getCacheDir());
			outputStream=new FileOutputStream(file);
			outputStream.write(editText.getText().toString().getBytes());
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(outputStream!=null){
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 读取内部存储的缓存文件
	 * @param view
	 */
	public void readCache(View view){
		StringBuilder sb=new StringBuilder();
		try {
File file3 = new File(getCacheDir(), "test11263086940.tmp");
 //File file=File.createTempFile("test178037233.tmp", null,getCacheDir());
			FileInputStream inputStream=new FileInputStream(file);
			BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
			String line=null;
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			textView.setText(sb.toString());
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除内部存储中的文件
	 * @param view
	 */
	public void delete(View view){
		boolean bl=deleteFile("info.txt");
		System.out.println("文件删除是否成功?"+bl);
	}
	

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值