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

前八天课程是基础中的重点:

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

开始时间: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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值