zoukankan      html  css  js  c++  java
  • Android学习笔记-保存数据的实现方法1

    Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考。

    第一种,保存数据到内存中:

    //java开发中的保存数据的方式
    public static boolean saveUserInfo(String username,String password){
    		File file = new File("/data/data/com.ftf.login/info.txt");
    		try {
    			FileOutputStream fos = new FileOutputStream(file);
    			// ftf##123
    			fos.write((username+"##"+password).getBytes());
    			fos.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    		
    	}
    
    //Android开发中,保存数据的方法,我们传递一个context对象,这样就可以较为直接的把数据保存到程序在手机系统中的单独的文件夹,符合Android的开发规范,
    	public static boolean saveUserInfo(Context context,String username,String password){
    		try {
    			
    			File filesDir = context.getFilesDir();
    			File file = new File(filesDir,"info.txt");
    			FileOutputStream fos = new FileOutputStream(file);
    			// ftf##123
    			fos.write((username+"##"+password).getBytes());
    			fos.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    		
    	}
    	/*
    	 * 获取保存的数据
    	 */
    	public static Map<String,String> getSavedUserInfo(Context context){
    		File filesDir = context.getFilesDir();
    		File file = new File(filesDir,"info.txt");
    		try {
    			FileInputStream fis = new FileInputStream(file);
    			//使用buffer,
    			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    			String str = br.readLine();
    			String[] infos = str.split("##");
    			Map<String,String> map = new HashMap<String, String>();
    			map.put("username", infos[0]);
    			map.put("password", infos[1]);
    			
    			return map;
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return null;
    		}
    		
    	}
    

     第二种,保存数据到SD卡

      这时我们需要用到Environment,来较为方便的获取SD卡的目录,这时随便一般情况下,SD卡是在/data/data/sdcard目录下,但是一些国产手机,以及平板中目录机构不是这样的,这样做可以保证程序的兼容性,而且也是Android开发规范推荐。

    
    
    public static boolean saveUserInfo(Context context,String username,String password){
    		try {
    			
    //			File filesDir = context.getFilesDir();
    //			File file = new File(filesDir,"info.txt");
    			if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));
    			//获取SD卡的目录
    			File  file = new File(Environment.getExternalStorageDirectory(),"info.txt");
    			
    			FileOutputStream fos = new FileOutputStream(file);
    			// ftf##123
    			fos.write((username+"##"+password).getBytes());
    			fos.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    		
    	}
    

     第三,按照权限,对文件进行存储

      这个较为符合Android的开发规范,Android下文件存储有四中类型:PRIVATE,READEABLE,WRITEABLE,READEABLE+WRITEABLE,也即私有,可读,可写,可读可写,我们在保存文件的时候可以直接进行指定,而且context可以直接打开一个文件输出流,所以Android下开发保存文件,推荐这种方式。

    	public static boolean saveUserInfo(Context context,String username,String password,int mode){
    		try {
    //			
    //			File filesDir = context.getFilesDir();
    //			File file = new File(filesDir,"info.txt");
    //			FileOutputStream fos = new FileOutputStream(file);
    			//在上下文的环境创建一个文件
    			FileOutputStream fos = null;
    			switch (mode) {
    			case 1:
    				fos = context.openFileOutput("private.txt", Context.MODE_PRIVATE);
    				break;
    			case 2:
    				fos = context.openFileOutput("readeable.txt", Context.MODE_WORLD_READABLE);
    				break;
    			case 3:
    				fos = context.openFileOutput("writeable.txt", Context.MODE_WORLD_WRITEABLE);
    				break;
    			case 4:
    				fos = context.openFileOutput("public.txt", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
    				break;
    			}
    			// ftf##123
    			fos.write((username+"##"+password).getBytes());
    			fos.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return false;
    		}
    		return true;
    	}
    
  • 相关阅读:
    import和require的区别
    React Native--Animated:`useNativeDriver`is not supported because the native animated module is missing
    XMLHttpRequest
    react-native中更改android/ios的入口文件
    react-native 跳转到ios/android 权限设置界面
    CocoaPods安装和使用
    React、React Native面试题
    UIBarButtonSystemItem 样式
    (转)自定义UITabBar
    ios 代码规范
  • 原文地址:https://www.cnblogs.com/fengtengfei/p/3961089.html
Copyright © 2011-2022 走看看