Android开发——内部存储数据(FileIntputStream和FileOutputStream)

  我们这周学习的是数据存储,第一个内容是内部存储数据。Java提供了一整套完整的IO流体系,包括FileIntputstream,FileOutputstream等,Android同样支持这样的方式从存储器访问文件。

  实现效果:

代码部分:

activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.datasave.MainActivity" >

    <EditText
        android:id="@+id/main_et_writedata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请写入数据" />
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="写入"
        android:onClick="write"/>
     
     <EditText
        android:id="@+id/main_et_readdata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请读取数据" />
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取"
        android:onClick="read"/>
</LinearLayout>

MainActivity.java

package com.example.datasave;

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

import com.example.datasave.Utils.ToastUtil;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.RequiresPermission.Write;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Squid
 *
 */
public class MainActivity extends Activity {

	private final String FILE_NAME="squid.txt";//文件名
	private EditText mEt_writeData,mEt_readData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mEt_writeData=(EditText)findViewById(R.id.main_et_writedata);
		mEt_readData=(EditText)findViewById(R.id.main_et_readdata);
	}
	
	//写入按钮的监听
	public void write(View view) {
		// TODO Auto-generated method stub
		String content=mEt_writeData.getText().toString();
		try {
			/*
			 * getFileDir:获取应用程序数据文件的绝对路径
			 * “/”表示路径名下一级目录
			 * 
			 */
			FileOutputStream out=new FileOutputStream(getFilesDir()+"/"+FILE_NAME);
			//将fos包装成PrintStream流
			PrintStream ps=new PrintStream(out);
			//向文件中写入数据
			ps.write(content.getBytes());
			//关闭PrintSteaml流
			ps.close();
			//关闭
			out.close();
			ToastUtil.showToast(this, "写入成功!");
			mEt_writeData.setText("");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ToastUtil.showToast(this, "写入失败");
		}
	}
	//读取按钮的监听
	public void read(View view) {
		// TODO Auto-generated method stub
		//
		try {
			//创建文件输入流
			FileInputStream fis=new FileInputStream(getFilesDir()+"/"+FILE_NAME);
			//创建byte数组
			byte[] by=new byte[1024];
			//定义读取长度
			int len=-1;
			//定义可变长字符串
			StringBuffer sb=new StringBuffer();
			//循环读取
			while ((len=fis.read(by))!=-1) {
				sb.append(new String(by,0,len));
			}
			ToastUtil.showToast(this, "读取成功!");
			//拿到读取的字符串
			String content=sb.toString();
			//显示在页面
			mEt_readData.setText(content);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ToastUtil.showToast(this, "读取失败!");
		}
	}
}

  这次的代码是不是觉得和以前的不一样了?代码注释明显清晰了!养成良好的注释习惯,争取下岗后还知道自己写过些什么东西。虽然离上岗还有一段时间。

补充一个封装好的toast

package com.example.datasave.Utils;

import android.content.Context;
import android.widget.Toast;

/**
 * @author Squid
 *
 */
public class ToastUtil {

	/**
	 * 显示的Toast内容
	 * @param context:上下文环境
	 * @param text:需要显示的内容
	 */
	public static void showToast(Context context,String text){
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游语

对你有帮助,可以请我喝杯奶哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值