标签:
1、输出堆栈信息到日志文件中
有时候,我们通过 IDE 在本地运行的时候,运行后报的错误可以在控制台看到详细的堆栈信息。但是当日志输出到文件中时,在遇到问题时,却不能显示具体的堆栈信息,很难发现具体的问题及解决问题。下面这个方法,是将报错后的堆栈信息输出到日志文件中。
/** * 输出堆栈信息到日志文件中 * @author gaoqing * 2015-2-1 * @param t 异常对象 * @return 堆栈信息 */ public static String getTrace(Throwable t) { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); t.printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); return buffer.toString(); } // 调用方法: try(){ }catch(IoException e) e.printStackTrace(); // logger.error(getTrace(e)); //将异常输出到文件 }
2、对 Double 类型的数据进行四舍五入
/** * 对 Double 类型的数据进行四舍五入 * @author gaoqing * 2014-12-2 * @param initValue 初始值 * @param scaleNum 保留小数点的位数 * @param tClass 保留小数点位数后,返回的数据类型的 Class * @return t 四舍五入后的值 */ public static <T> T roundDouleValue(Double initValue, int scaleNum, Class<T> tClass){ T t = null; String className = tClass.getName().toLowerCase(); if (initValue == null) { //返回类型为:整数 if (className.contains("int")) { t = (T) Integer.valueOf(0); //返回类型为:浮点数 }else if (className.contains("dou")) { t = (T) Double.valueOf(0.0); } }else { BigDecimal bigDecimal = new BigDecimal(initValue); BigDecimal scaledBigDecimal = bigDecimal.setScale(scaleNum, BigDecimal.ROUND_HALF_UP); //返回类型为:整数 if (className.contains("int")) { Integer tempInteger = new Integer(scaledBigDecimal.intValue()); t = (T)tempInteger ; //返回类型为:浮点数 }else if (className.contains("dou")) { Double tempDouble = new Double(scaledBigDecimal.doubleValue()); t = (T) tempDouble; } } return t; }
3、判断当前运行环境中,是否有指定名称的线程在执行
/** * 得到当前运行的所有线程集 * 2015-02-02 * @author gaoqing * @return threads 当前运行的所有线程集 */ private static Thread[] getThreads(){ //得到当前线程所在的组 ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup(); //线程组的顶级组对象 ThreadGroup topThreadGroup = currentThreadGroup; //得到线程组的顶级组对象 while (currentThreadGroup != null) { topThreadGroup = currentThreadGroup; currentThreadGroup = currentThreadGroup.getParent(); } //预估线程组的线程个数 int preTopThreadGroupSize = topThreadGroup.activeCount() * 2; Thread[] preThreads = new Thread[preTopThreadGroupSize]; //将线程组中的线程复制到 preThreads 中 int actualThreadSize = topThreadGroup.enumerate(preThreads); Thread[] actualThreads = new Thread[actualThreadSize]; //将预估线程集中的线程,复制到真实大小的线程集中 System.arraycopy(preThreads, 0, actualThreads, 0, actualThreadSize); return actualThreads; }
/** * 判断指定的线程是否存在 * @author 高青 * 2015-2-2 * @param threadName 线程名称 * @return isJudgeSpecifyThreadExist 指定的线程是否存在标识(默认:false,不存在) */ public static boolean judgeSpecifyThreadExist(String threadName){ boolean isJudgeSpecifyThreadExist = false; //得到所有运行的线程集 Thread[] threads = ThreadUtils.getThreads(); if (threadName != null && threadName.length() != 0) { if (threads != null && threads.length != 0) { for (Thread thread : threads) { if (threadName.equals(thread.getName())) { isJudgeSpecifyThreadExist = true; } } } } return isJudgeSpecifyThreadExist; }
4、得到指定 URL 地址中的内容
public static String getURLContent(String url) { String str_data = ""; URL urlobj = null; HttpURLConnection connection = null; Reader reader = null; try { urlobj = new URL(url); connection = (HttpURLConnection) urlobj.openConnection(); //判断当前链接是否可用 int responseCode = connection.getResponseCode(); if (responseCode == 200) { //connection.setRequestMethod("POST"); //connection.setDoOutput(true); //connection.setUseCaches(false); connection.connect(); InputStream inputStream = connection.getInputStream(); reader = new InputStreamReader(inputStream, charset); BufferedReader bufferedReader = new BufferedReader(reader); String str = null; StringBuffer sb = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { sb.append(str.trim()); } str_data = sb.toString(); // 判断 str_data 是否为空,如果为空,则输出为空的 URL if ("".equals(str_data) || str_data.isEmpty()) { log.info(url + " 的链接内容是空!"); } if (reader != null) { try { reader.close(); } catch (IOException e) { } } if (connection != null) { connection.disconnect(); } }else { log.info("当前链接:" + url + " 不可用!"); } } catch (IOException e) { log.info("当前链接:" + url + " 不可用!"); e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } if (connection != null) { connection.disconnect(); } } return str_data; }
5、深度复制对象
/* * 1、实例化一个对象 * 2、将当前对象序列化到对象流中 * 3、将当前对象,从对象流中取出 * 4、输出深复制后对象的信息 */ //1、实例化一个对象 SerializableObj initialObj = new SerializableObj("gaoqing", new Integer(29)); //2、将当前对象序列化到对象流中(数据最终存放在最底层的流中) ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(initialObj); //3、将当前对象,从对象流中取出 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); SerializableObj seriObj = (SerializableObj)ois.readObject(); System.out.println(seriObj);
标签:
原文地址:http://www.cnblogs.com/gaoqing/p/4383957.html