您的位置:首页 > 移动开发 > Android开发

android_before_day08_小结_文件保存三种方式

2013-03-10 15:30 423 查看
前八天课程是基础中的重点:

有时间总结下每天的内容,先总体总结下:

开始时间:2013年3月10日15:28:40

day02 android保存文件三种方式(其他方式暂时没学)

布局文件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=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />

<Button
android:onClick="save2ROM"
android:id="@+id/bt_save2rom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save2rom" />

<Button
android:onClick="save2SD"
android:id="@+id/bt_save2rom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save2sd" />

<Button
android:onClick="save2SP"
android:id="@+id/bt_save2sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到sp" />

</LinearLayout>


工具类:从输入流读取返回读取的文本:

/**
* 工具类:将输入流转换成String 返回
* @param input 需要转换的输入流
* @return 返回输入流中读取的文本
* @throws Exception
*/
public static  String  stream2String(InputStream input) throws Exception {

//		创建数组输出流
ByteArrayOutputStream byetarrayout = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int leng =-1;
while((leng = input.read(buff))!=-1){
byetarrayout.write(buff);//写入到数组删除流,方便转换成string;
}
//获得String
String info = byetarrayout.toString();
byetarrayout.close();
input.close();
return info;
}


保存到rom:

// 保存到rom
public void save2ROM(View view) {

try{
// 获得本地输出流
FileOutputStream output = getApplicationContext().openFileOutput("rom.txt", MODE_WORLD_READABLE);
//		写入数据
output.write("这是写到本地内存的里的文字啊。。。。。".getBytes());
Toast.makeText(getApplicationContext(), "保存到ROM卡完毕", 1).show();

//		读取文件获得输入流
FileInputStream input = getApplicationContext().openFileInput("需要读取文件名称");
//		模板代码输入流转换到string,可以写成工具类:
//		创建数组输出流
ByteArrayOutputStream byetarrayout = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int leng =-1;
while((leng = input.read(buff))!=-1){
byetarrayout.write(buff);//写入到数组删除流,方便转换成string;
}
//获得String
String info = byetarrayout.toString();
byetarrayout.close();
input.close();

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "保存到ROM卡失败", 1).show();
}
}


保存到SDcard

// 保存到SDcard
public void save2SD(View view) {

// 获得路径
try {
File file = Environment.getExternalStorageDirectory();
File savefile = new File(file, "sd.txt");
FileOutputStream out = new FileOutputStream(savefile);
out.write("这是写到内存卡里的文字啊。。。。。".getBytes());
Toast.makeText(getApplicationContext(), "保存到SD卡完毕", 1).show();

//读取文件:
FileInputStream readfile = new FileInputStream(file);
//模板代码
String readinfo = stream2String(readfile);
Toast.makeText(getApplicationContext(), readinfo, 1);

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "保存到SD卡失败", 1);
}

}


保存到:sharepreference

// 保存到sharepreference
public void save2SP(View view) {

// 获得sharepreference
try {
SharedPreferences preferences = this.getSharedPreferences("sp.txt",
MODE_WORLD_WRITEABLE);
Editor editor = preferences.edit();
editor.putString("内容", "这是写入到shareperf内容");
// HashSet<String> set = new HashSet<String>();
// set.add("setlist");
// set.add("写入到sp");
// editor.putStringSet("set集合",set);
editor.commit();// 提交数据
Toast.makeText(getApplicationContext(), "写入到sp完毕", 1).show();

// 获取数据:
preferences.getString("需要读取的Key", "没有读取到的默认返回数据");
String string = preferences.getString("内容", "没有获得数据,这是默认数据");
Toast.makeText(getApplicationContext(), string, 1).show();

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "保存到SP卡失败", 1);
}
}


2013-3-10 15:32:13
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: