自己的一个小练习,记录一下

package cn.com.xy.galaxy;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.Date;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Exercise {public static void main(String args[]) {// String userId = "galaxy1";// List list = getUserNames(userId);// for(int i=0; i<list.size(); i++) {// System.out.println(i + ":" + list.get(i));// } String templateString = "select * from S(TABLE_NAME) where ID=S(ROW_ID)";// Map parameters = new HashMap();// parameters.put("TABLE_NAME", "pro_user");// parameters.put("ROW_ID", "liube");// parameters.put("", "sdf");// System.out.println(getNewStringByTemplateAndParams(templateString, parameters));System.out.println(toUpper(3));Map map = new HashMap();map.put("k1", 123342L);map.put("k2", 324);map.put("k3", 324.543);map.put("k4", 344354d);System.out.println("maxKey:" + getMaxKey(map));String fileFullPath = "";fileFullPath = "F:/Temp/export.log";fileFullPath = "F:/会计制度复习题.txt";String content = readFile(fileFullPath);System.out.println("file content:" + content);System.out.println("decode:" + deUnicode("4e2d6587"));fileFullPath = "F:/会计制度复习题1.txt";System.out.println("last modified time:" + getModifyTime(fileFullPath));}public static String deUnicode(String content) {// 将16进制数转换为汉字String enUnicode = null;String deUnicode = null;for (int i = 0; i < content.length(); i++) {if (enUnicode == null) {enUnicode = String.valueOf(content.charAt(i));} else {enUnicode = enUnicode + content.charAt(i);}if (i % 4 == 3) {if (enUnicode != null) {if (deUnicode == null) {deUnicode = String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());} else {deUnicode = deUnicode+ String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());}}enUnicode = null;}}return deUnicode;}public static String enUnicode(String content) {// 将汉字转换为16进制数String enUnicode = null;for (int i = 0; i < content.length(); i++) {if (i == 0) {enUnicode = getHexString(Integer.toHexString(content.charAt(i)).toUpperCase());} else {enUnicode = enUnicode+ getHexString(Integer.toHexString(content.charAt(i)).toUpperCase());}}return enUnicode;}private static String getHexString(String hexString) {String hexStr = "";for (int i = hexString.length(); i < 4; i++) {if (i == hexString.length())hexStr = "0";elsehexStr = hexStr + "0";}return hexStr + hexString;}/** * 将数值类型的value转换成Double类型。若value不是数值类型,则返回null * @param value * @return */private static Object getDouble(Object value) {Double valueD;if(value instanceof Float) {valueD = Double.parseDouble(Float.toString((Float) value));} else if(value instanceof Byte) {valueD = (double) (Byte) value;} else if(value instanceof Short) {valueD = (double) (Short) value;} else if(value instanceof Integer) {valueD = (double) (Integer) value;} else if(value instanceof Long) {valueD = (double) (Long) value;} else if(value instanceof Double) {valueD = (Double) value;} else {return null;}return valueD;}/** * 获取map中值最大的数字对应的key。若map中value的数据类型不是数值类型,则忽略、不进行比较 * * @param map * @return */public static String getMaxKey(Map map) {Set keys = map.keySet();Iterator it = keys.iterator();String maxKey = null;Double maxValue = 0d;while(it.hasNext()) {String key = it.next();Object valueObj = map.get(key);if(maxKey == null) {Object valueCur = getDouble(valueObj);if(valueCur == null){continue;} else {maxKey = key;maxValue = (Double) valueCur;}continue;}if(valueObj == null) {continue;}Object valueCur = getDouble(valueObj);if(valueCur != null && maxValue < (Double) valueCur){maxValue = (Double) valueCur;maxKey = key;}}return maxKey;}public static String getModifyTime(String fileFullPath) {if(fileFullPath == null || fileFullPath.equals("")) {return null;}// fileFullPath = fileFullPath.replaceAll("\\", "/");String folder = fileFullPath.substring(0, fileFullPath.lastIndexOf("/"));File folderFile = new File(folder);if(!folderFile.exists()) {folderFile.mkdir();}File file = new File(fileFullPath);long lastModified = -1;if(!file.exists()) {try {file.createNewFile();lastModified = file.lastModified();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {lastModified = file.lastModified();}String lastModifiedTime = null;if(lastModified > 0) {String dateFormat = "yyyyMMddkkmmssSSS";SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);Date date = new Date(lastModified);lastModifiedTime = sdf.format(date);}return lastModifiedTime;}/** * 根据模板获取对应sql语句 * * @param templateString 模板sql * @param parameters 参数 * @return */public static String getNewStringByTemplateAndParams(String templateString, Map parameters) {String regex = "S\\([\\w]*\\)"; //匹配'[A-Za-z0-9_]'Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(templateString);while(matcher.find()) {String temp = templateString.substring(matcher.start(), matcher.end());// if(temp.equals("S()")) {// return null;// }String key = temp.substring(2, temp.length() - 1);templateString = templateString.replaceAll("S\\(" + key + "\\)", parameters.get(key)); //转移(、)字符matcher = pattern.matcher(templateString); //因原字符串已变动,需重新匹配}return templateString;}/** * 根据人员ID获取所有对应的姓名 * * @param userId 人员ID * @return 人员姓名列表 */public static List getUserNames(String userId) {List nameList = new ArrayList();String sql = "SELECT NAME FROM PRO_USER WHERE ID = ?";Connection conn = getConnection();PreparedStatement pstmt = null;ResultSet rs = null;try {pstmt = conn.prepareStatement(sql);pstmt.setString(1, userId);rs = pstmt.executeQuery();while(rs.next()) {nameList.add(rs.getString("NAME"));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if(conn != null) {conn.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return nameList;}public static String readFile(String fileFullPath) {String content = null;File file = new File(fileFullPath);BufferedReader reader = null;try {reader = new BufferedReader(new InputStreamReader( new FileInputStream(file), "UTF-8"));String tmpStr = null;while((tmpStr = reader.readLine()) != null) {if(content == null) {content = tmpStr;} else {content += "\n" + tmpStr;}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if(reader != null) {reader.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return content;}// 转化十六进制编码为字符串public static String toStringHex(String s) {byte[] baKeyword = new byte[s.length() / 2];for (int i = 0; i < baKeyword.length; i++) {try {baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));} catch (Exception e) {e.printStackTrace();}}try {s = new String(baKeyword, "utf-8");// UTF-16le:Not} catch (Exception e1) {e1.printStackTrace();}return s;}// public static void main(String[] args) {// System.out.println(encode("中文"));// System.out.println(decode(encode("中文")));// } /** * 将数字转换成大小格式并加单位元 * * @param amout * @return */public static String toUpper(int amout) {if(amout <= 0) {return null;}String aLower = Integer.toString(amout);String aUpper = "";String[] values = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};String[] unites = {"十", "百", "千", "万"};int length = aLower.length();if(length == 1) {aUpper = values[amout];} else if(length >= 2) {if(amout == 10) {aUpper = unites[0];} else if(amout < 20) {aUpper = unites[0] + values[aLower.charAt(1) - '0'];} else {boolean flag = false;for(int i=0; i<aLower.length(); i++) {if(aLower.charAt(i) != '0') {aUpper += values[aLower.charAt(i) - '0'];if(i < length - 1) {if(length - i > length) {return null;}aUpper += unites[length - i - 2];flag = false;}} else if(!flag){aUpper += values[aLower.charAt(i) - '0'];flag = true;}}// aUpper = values[aLower.charAt(0) - '0'] + unites[length - 2] + values[aLower.charAt(1) - '0'];}}aUpper += "元";return aUpper;}/** * 获取数据库连接 * @return */public static Connection getConnection() {String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";String driverName = "oracle.jdbc.driver.OracleDriver";String userName = "XY";String password = "123456";Connection conn = null;try {Class.forName(driverName);conn = DriverManager.getConnection(dbUrl, userName, password);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值