我们这周学习的是数据存储,第一个内容是内部存储数据。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();
}
}