task2 - sarasayhi/hello-world GitHub Wiki
Map<String,AnimalBean> map = new HashMap<>(); String path = Thread.currentThread().getContextClassLoader().getResource("1111111.txt").getPath(); ObjectOutputStream objOut = null; ObjectInputStream objIn = null; try { objOut =new ObjectOutputStream(new FileOutputStream(new File(path))); map.put("1",new AnimalBean("1","1",2)); map.put("2",new AnimalBean("2","1",2)); map.put("3",new AnimalBean("3","1",2) ); objOut.writeObject(map); objOut.flush(); System.out.println("write object success!"); objIn =new ObjectInputStream(new FileInputStream(new File(path))); Map<String,AnimalBean> readMap=(Map<String,AnimalBean>)objIn.readObject();
public class DBCacheService { private static Map<String, User> dbCache = null;
private DBCacheService(){
}
public static void init(){
if (null == dbCache) {
synchronized(DBCacheService.class){
if (null == dbCache){
Map<String, User> userList = DBUtil.readJson();
if(null == userList){
dbCache = new HashMap<>();
}
}
}
}
}
public static void set(){
}
public static void get(){
}
public static void contains(){
}
}
public static boolean isNullOrEmpty(String str) { return str == null || str.isEmpty(); }
public static boolean isNullOrEmpty(String str) { return str == null || str.isEmpty(); }
public static void writeJsonFile(String filePath, String sets) throws IOException {
FileWriter fw = new FileWriter(filePath);
PrintWriter out = new PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
}
public static String ReadJsonFile(String path) { File file = new File(path); BufferedReader reader = null; String laststr = ""; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; while ((tempString = reader.readLine()) != null) { laststr = laststr + tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) {} } } return laststr; }
public static Map<String, Integer> userStatistics(Map<String, Integer> result, String[] list, int current, int compare) { int init = current; while (compare <= list.length - 1) { if (list[compare].equals(list[current])) { String tmp = list[compare]; current++; list[compare] = list[current]; list[current] = tmp; } compare++; } result.put(list[current], current - init);
if (current == list.length - 1) {
return result;
} else {
compare = current + 2;
current = current + 1;
return userStatistics(result, list, current, compare);
}
}
public static String getMaxOFMapValue(Map<String, Integer> resMap) { String max = null; for (String s : resMap.keySet()) { if (max == null) { max = s; } else { if (resMap.get(max) < resMap.get(s)) { max = s; } } } return max; }
private static String fileName = "userList.json";
private static String URL = DBUtil.class.getClassLoader().getResource(fileName).getPath();
public static String getURL(){ // D:/eclipse_dev_projects/mine/helloWorld/target/classes/text.json int index = URL.indexOf("/"); return URL.substring(index + 1, URL.length()); }
public static Map<String, User> readData(){
return null;
}
public static UserList readJson(){ String str = ReadJsonFile(getURL()); if(isNullOrEmpty(str)){ return null; } return JSON.parseObject(str,UserList.class); }
public static void writeJson(UserList list){
try {
writeJsonFile(getURL(), JSON.toJSONString(list));
} catch (IOException e) {
System.out.println("用户信息写入json文件错误");
e.printStackTrace();
}
}
public static String getFileName() { return fileName; }
public static void setFileName(String fileName) { DBUtil.fileName = fileName; }
public static void setURL(String uRL) { URL = uRL; }
UserDao userDao = new UserDao();
@Override public boolean checkUserName(String userName) { boolean exist = false; List list = userDao.getAllUser(); if(null == list){ return exist; } for(User u:list){ if(userName == u.getUserName()){ exist = true; } } return exist; }
@Override public boolean registerUser(User user) { boolean isSuccess = false; if(null == user){ return isSuccess; }
if(null != user.getUserName()&&null !=user.getPassword()){
user.setBingoNum(0);
userDao.insertUser(user);
}
return isSuccess;
}
@Override public User login(User user) { String userName = user.getUserName(); if(userName.isEmpty()){ return null; } return userDao.getUser(user.getUserName()); }