我的Java工具类(不断更新)

风流意气都作罢 提交于 2020-01-11 22:43:03

Scanner(要求用户按要求输入int double 数据)


//对输入整数进行优化
public class ScannerUtils {
    //私有构造方法
    private ScannerUtils(){

    }
    public static int getInt() {
        while (true) {
                Scanner sc = new Scanner(System.in);
            try {//不搞录入语句了 一般都需要按照题目要求写
                return sc.nextInt();
            } catch (Exception e) {
                System.out.println("别乱搞");
                System.out.println("再搞让你蓝屏");
            }
        }
    }

    public static double getDouble(){
        while (true) {
            Scanner sc = new Scanner(System.in);
            try {
                System.out.println("输入个数!!!!");
                return sc.nextDouble();
            } catch (Exception e) {
                System.out.println("别乱搞");
                System.out.println("再搞让你蓝屏");
            }
        }
    }
}

复制文件(为了通用性,采用字节流进行复制


public class CopyUtils {
    public CopyUtils() {
    }

    public static void copyFile(String a, String b){
        try( BufferedInputStream bis = new BufferedInputStream(new FileInputStream(a));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(b))) {
            byte[] bytes = new byte[1024];
            int len;
            while((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Date(将date与String进行格式转换)


public class DateUtils {
    private DateUtils() {
    }

    public static String dateToString(Date d,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(d);
        return s;
    }

    public static Date stringToDate(String str, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = sdf.parse(str);
        return d;
    }

}

Reflect

public class BeanUtils {

    public static void setProperty(Object o, String name, Object value)  {
        try {
            Class<?> c = o.getClass();
            Field f = c.getDeclaredField(name);
            f.setAccessible(true);
            f.set(o, value);
        } catch (Exception e) {
            System.out.println("友情提示,赋值不成功");
        }
    }

    public static Object getProperty(Object o, String name){
        try {
            Class<?> c = o.getClass();
            Field f = c.getDeclaredField(name);
           f.setAccessible(true);
            return f.get(o);
        } catch (Exception e) {
            System.out.println("友情提示,取值失败");
             return null;
        }


    }

    public static void fill(Object o, Map<String,Object> map) {
        Class<?> c = o.getClass();
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            try{
                Field f = c.getDeclaredField(entry.getKey());
                f.setAccessible(true);
                f.set(o,entry.getValue());
            }catch (Exception e){
                System.out.println("友情提示,有一个值没有赋上哦");
            }

        }
    }
}

JDBC(获取连接&释放资源)

properties配置文件:
url=jdbc:mysql://localhost:3306/day4
userID=xxxx
password=xxxx
driver=com.mysql.jdbc.Driver
/*
获取src路径下的文件的方式----> ClassLoader 类加载器

        ClassLoader cl = utils.class.getClassLoader();
    URL res = cl.getResource("ConnectionUse.properties");
    String path = res.getPath();
FileReader fr = new FileReader(path);
 */

public class utils {
    private static String url;
    private static String userID;
    private static String password;
    private static String driver;

    static {
        FileReader fr = null;
        try {
            Properties pro = new Properties();
            fr = new FileReader("D:\\develop\\ideaaaaa\\JDBCuse\\JDBC_utils\\ConnectionUse.properties");
            pro.load(fr);
            url = pro.getProperty("url");
            userID = pro.getProperty("userID");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            Class.forName(driver);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();

        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,userID,password);
    }

    //释放资源
    public static void close(Statement stmt, Connection con) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //释放资源(重载)
    public static void close(ResultSet rs, Statement stmt, Connection con) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!