下面以在项目本地目录中保存用户的账号和密码为例:
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();
}
}
}