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();
 }
}
//-------------------------------------------------------------------------------------

时间: 2024-08-01 14:44:45

Java 常用方法搜集 欢迎补充的相关文章

爬虫5 scrapy框架2 全站爬取cnblogs, scarpy请求传参, 提高爬取效率, 下载中间件, 集成selenium, fake-useragent, 去重源码分析, 布隆过滤器, 分布式爬虫, java等语言概念补充, bilibili爬视频参考

1 全站爬取cnblogs # 1 scrapy startproject cnblogs_crawl # 2 scrapy genspider cnblogs www.cnblogs.com 示例: # cnblogs_crawl/cnblogs_crawl/spiders/cnblogs.py import scrapy from cnblogs_crawl.items import CnblogsCrawlItem from scrapy.http import Request class

java常用方法

1. byte[] b = {1,2,3,4,5,6,7}; byte[] c = new byte[5]; void System.arraycopy(b,2,c,0,c.length); //后5个 2. new int[1].length; new String("1").length(); 3.Object java.lang.Object :11 protected Object clone()throws CloneNotSupportedException public

22 java常用方法

/** * 通过正则获取该目录下满足条件的所有目录 * @param luceneFilePathRegular 正则目录,如/user/solrindex/正则表达式 * @return 满足正则表达式的目录集合 list */ public static List<String> fetchDirByRegularLinux(String luceneFilePathRegular){ List<String> list=new ArrayList<>(); //分

Java培训-IO流补充

1.RandomAccessFile: 支持文件读写. 构造器 RandomAccessFile(String file,String mode) Mode: r  读 rw 读写 read()-. Write()-. Seek(long pos) 跳过pos字节,pos+1开始读取或写入 skipBytes(int n)丢弃n个字节,不抛异常 public void rw(){           try {               RandomAccessFile raf=new Ran

Java的Iterator迭代器补充,增强for循环,泛型,List接口,set接口

1.Iterator迭代器:(1)类型转换异常:ClassCastException:集合中存放的是多个对象时,在强转时会出现: package com.oracle.demo01; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class demo01 { public static void main(String[] args) { method03();

java第三章补充

1.循环结构的语法有哪些?while(){} do{}while(); for(){}2.循环的几个要素有哪些?4个循环初始值,循环条件(对初始值的判断),循环体,循环的退出3.各种循环结构有什么特点?while:先判断,后执行do{}while()先执行一次,再判断for()先判断后执行 for循环里面的定义的值只能在for里面使用例如:这断错误的代码 编译会出现错误,想要纠正错误必须把int i 放在for循环外面声明 break:结束所在循环,后续操作不执行,整个循环结束continue:

java 常用方法

一.数组 数组的声明: 方式一: String [] str1=new String[10]; 方式二: String [] str2={"A","v"}; 1.把一个大数组分割为n个小数组 public static <T> List<Object[]> arraySpl(T [] str,int num){ if(str==null||str.length==0){ throw new NullPointerException("

java中equals和“==”补充

在JDK1.5之后,Integer添加了自动装箱,其形式为Integer i = 5; 装箱过程在内存中是  i = new Integer(4);大家都很熟悉这个吧. 当使用这中形式的时候,equals的用法不变,但是"=="略有不同 看下边的例子: (1) Integer x = 12; Integer y = 12; System.out.println(x==y); //true System.out.println(x.equals(y)); //true (2) Integ

Java架构搜集

1. 2.