- package com.it.login.service;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import android.content.Context;
- public class LoginService {
- /**
- * 保存文件
- * @param context 上下文
- * @param username
- * @param password
- * @return
- */
- public static boolean saveUserInfo(Context context,String username,String password){
- File file=new File(context.getFilesDir(),"user.bat"); //在当前包下,创建文件
- try {
- FileOutputStream fis = new FileOutputStream(file);
- fis.write((username+"##"+password).getBytes());
- fis.close();
- return true;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 回显用户名 密码
- * @param context
- * @return
- */
- public static Map<String,String> getUserInfo(Context context){
- File file=new File(context.getFilesDir(),"user.bat");
- try {
- Map<String,String> map=new HashMap<String, String>();
- FileInputStream fis = new FileInputStream(file);
- BufferedReader br=new BufferedReader(new InputStreamReader(fis));
- String str=br.readLine();
- String[] infos=str.split("##");
- map.put("username", infos[0]);
- map.put("password", infos[1]);
- return map;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2404201512433.html
来源: http://www.codesnippet.cn/detail/2404201512433.html