Android中,在项目文件目录中保存一些数据的几种方法

下面以在项目本地目录中保存用户的账号和密码为例:

1、通过输入输出流实现对绝对路径下文件的读写

public class UserInfoUtils {
/将用户信息写入指定目录/
public static boolean saveInfo(Context context,String username,String password){
try {
String result = username + “##” +password;
File file = new File(“data/data/com.Luo.wordmemory/info.txt”);
FileOutputStream fos = new FileOutputStream(file);
fos.write(result.getBytes());
fos.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
}
/验证已注册用户/
public static boolean verifyUserInfo(Context context,String username,String password){
String line="";
String info=username+"##"+password;
try {
File file = new File(“data/data/com.Luo.wordmemory/info.txt”);
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(reader);
/读整个文件的内容/
while ((line = bufferedReader.readLine()) != null) {
}
fis.close();
bufferedReader.close();
if(line.equals(info)){
return true;
}else {
return false;
}
} catch (Exception e) {
Log.e(“verifyUserInfo”,“验证已注册用户时,出现异常”);
}
return false;
}
}

2、对输入输出流读写文件的改进,使用上下文获取文件保存目录

public class UserInfoUtils_1 {
/将用户信息写入指定目录/
public static boolean saveInfo(Context context,String username,String password){
try {
//使用上下文获取文件保存目录
String path =context.getFilesDir().getPath();
String result = username + “##” +password;
File file = new File(path,“info.txt”);
FileOutputStream fos = new FileOutputStream(file);
fos.write(result.getBytes());
fos.close();
return true;
} catch (Exception e) {
return false;
}
}
/验证已注册用户/
public static boolean verifyUserInfo(Context context,String username,String password){
String line="";
String info=username+"##"+password;
try {
String path =context.getFilesDir().getPath();
File file = new File(path,“info.txt”);
FileInputStream fis =new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
}
if(line.equals(info)){
return true;
}else {
return false;
}
} catch (Exception e) {
Log.e(“verifyUserInfo”,“验证已注册用户时,出现异常”);
}
return false;
}
}

3、使用上下文获取文件保存目录的改进

public class UserInfoUtils_2 {
/将用户信息写入指定目录/
public static boolean saveInfo(Context context,String username,String password){
try {
String result = username + “##” +password;
FileOutputStream fos = context.openFileOutput(“info.txt”, 0);
fos.write(result.getBytes());
fos.close();
return true;
} catch (Exception e) {
return false;
}
}

/验证已注册用户/
public static boolean verifyUserInfo(Context context,String username,String password){
String line="";
String info=username+"##"+password;
try {
FileInputStream fis = context.openFileInput(“info.txt”);
InputStreamReader reader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
}
if(line.equals(info)){
return true;
}else {
return false;
}
} catch (Exception e) {
// TODO: handle exception
Log.e(“verifyUserInfo”,“验证已注册用户时,出现异常”);
}
return false;
}
}
以上方法都是在一个工具类的基础上进行的,下面介绍一个不需要写工具类的方法。

4、运用SharedPreferences API来存信息

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

}
public void loginCheck(String username,String password){
	if(UserInfoUtils.checkInfo(MainActivity.this)){
		//直接跳到mainpage_1.xml
	}else {
		//注册用户
		//UserInfoUtils.saveInfo(MainActivity.this,username, password);
		//使用SharedPreferences去保存数据
		/*【1】第一步,拿到sp的实例
		 * 第一个参数会帮助生成一个xml文件
		 */
		SharedPreferences sp = getSharedPreferences("config", 0);
		//【2】获取sp的编辑器
		Editor editor = sp.edit();
		//【3】存数据。根据不同数据类型选择不同的put方法,
		editor.putString("username", username);
		editor.putString("password", password);
		//【4】最后一步,将编辑器提交
		editor.commit();
	}
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值