当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考。第一种,保存数据到内存中://java开发中的

 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

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

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;

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭