首页 > 代码库 > Java 常用方法搜集 欢迎补充
Java 常用方法搜集 欢迎补充
public static int inputChoose(String m) {//返回整形
Scanner sc = new Scanner(System.in);
System.out.print(m + ":");
return sc.nextInt();
}
//---------------------------------------------------------------------------
public static String inputAccount(){//输入账号
Console c = System.console();
return c.readLine("请输入账号:");
}
//---------------------------------------------------------------------------
public static void sleep(int i ){//系统等待
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//---------------------------------------------------------------------------
public static void writeUser(User u){//把对象放入Map并写入文件
HashMap<String, User> map = new HashMap<String,User>();
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("bank/user.dat"));
try {
map = (HashMap<String,User>)ois.readObject();
map.put(u.getCd(), u);
ois.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("bank/user.dat"));
oos.writeObject(map);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//---------------------------------------------------------------------------
public static String inputPassword(String m){//console输入密码
Console c = System.console();
return new String(c.readPassword(m+":"));
}
//---------------------------------------------------------------------------
public static String input(){//输入 String
Scanner sc =new Scanner(System.in);
return sc.next();
}
//---------------------------------------------------------------------------
public static String inputPassword(){//console输入密码//控制台输入!eclipse会报异常
Console c = System.console();
return new String(c.readPassword("请输入密码:"));
}
//---------------------------------------------------------------------------
public static int inputChoose(){//返回值int类型
Scanner sc = new Scanner(System.in);
System.out.print("请选择:");
return sc.nextInt();
}
//---------------------------------------------------------------------------
public static String md(String md,String pass){//密码加密 MD5方法
MessageDigest m;
String passok = "";
try {
m = MessageDigest.getInstance(md);
m.update(pass.getBytes());
for (byte b : m.digest()) {
passok += String.format("%x", b);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return passok;
}
//---------------------------------------------------------------------------
public static boolean authenticationKey(String cd){// 用户密码验证
System.out.println("请输入密码");
String newKey = md("MD5",input());
String oldKey = outUser(cd).getKey();
if(newKey.equals(oldKey)){
return true;
}else{
return false;
}
}
//-------------------------------------------------------
@SuppressWarnings("unchecked")
public static void removeUser(User user){//删除对象
HashMap<String, User> map = new HashMap<String,User>();
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("bank/user.dat"));
try {
map = (HashMap<String,User>)ois.readObject();
map.remove(user.getCd());
ois.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("bank/user.dat"));
oos.writeObject(map);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//-------------------------------------------------------------------------------------
Java 常用方法搜集 欢迎补充