day2_Android文件操作

1.文件输入输出流

1.1写文件到手机内存

public static boolean saveUserInfo(Context context, String number, String password) {
		
		try {
//			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
			
//			File filesDir = context.getFilesDir();
			File filesDir = context.getCacheDir();
			
			File f = new File(filesDir, "itheima28.txt");
			
			FileOutputStream fos = new FileOutputStream(f);
			
			// 307966990##123123
			String data = number + "##" + password;
			
			fos.write(data.getBytes());
			
			fos.flush();
			
			fos.close();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
1.2从手机内存读文件

public static Map<String, String> getUserInfo(Context context) {

		try {
//			String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
			
//			File filesDir = context.getFilesDir();
			File filesDir = context.getCacheDir();
			
			File f = new File(filesDir, "itheima28.txt");
			
			FileInputStream fis = new FileInputStream(f);
			
			// 字符流对象
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

			// 307966990##123123
			String text = reader.readLine();
			
			if(!TextUtils.isEmpty(text)) {
				String[] split = text.split("##");
			
				Map<String, String> userInfoMap = new HashMap<String, String>();
				userInfoMap.put("number", split[0]);
				userInfoMap.put("password", split[1]);
				return userInfoMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
1.3写文件到SD卡

public static boolean saveUserInfo(Context context, String number, String password) {
		
		try {
			// 判断当前的手机是否有sd卡
			String state = Environment.getExternalStorageState();
			
			if(!Environment.MEDIA_MOUNTED.equals(state)) {
				// 已经挂载了sd卡
				return false;
			}
			
			File sdCardFile = Environment.getExternalStorageDirectory();
			
			File file = new File(sdCardFile, "itheima28.txt");
			
			FileOutputStream fos = new FileOutputStream(file);
			
			String data = number + "##" + password;
			
			fos.write(data.getBytes());
			
			fos.flush();
			
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
1.4从SD卡读文件

public static Map<String, String> getUserInfo(Context context) {
		
		try {
			// 判断当前的手机是否有sd卡
			String state = Environment.getExternalStorageState();
			
			if(!Environment.MEDIA_MOUNTED.equals(state)) {
				// 已经挂载了sd卡
				return null;
			}
			
			File sdCardFile = Environment.getExternalStorageDirectory();
			
			File file = new File(sdCardFile, "itheima28.txt");
			
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			
			String text = br.readLine();
			
			br.close();
			
			if(!TextUtils.isEmpty(text)) {
				Map<String, String> userInfoMap = new HashMap<String, String>();
				String[] split = text.split("##");
				userInfoMap.put("number", split[0]);
				userInfoMap.put("password", split[1]);
				return userInfoMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

2.SharedPreferences存取文件

2.1存文件

public static boolean saveUserInfo(Context context, String number, String password) {
		
		try {
			// /data/data/包名/shared_prefs/itheima28
			SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE);
			
			// 获得一个编辑对象
			Editor edit = sp.edit();
			
			// 存数据
			edit.putString("number", number);
			edit.putString("password", password);
			
			// 提交, 数据真正存储起来了.
			edit.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
2.2 取文件

public static Map<String, String> getUserInfo(Context context) {
		
		SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE);
		
		String number = sp.getString("number", null);
		String password = sp.getString("password", null);
		
		if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) {
			Map<String, String> userInfoMap = new HashMap<String, String>();
			userInfoMap.put("number", number);
			userInfoMap.put("password", password);
			return userInfoMap;
		}
		return null;
	}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值