[关闭]
@xuduochoua 2015-11-17T04:05:31.000000Z 字数 7342 阅读 1244

VIC常用工具类

工具类 java

  1. 一些项目中常用的java工具类

1. JSON工具类

  1. import java.io.IOException;
  2. import org.codehaus.jackson.JsonGenerationException;
  3. import org.codehaus.jackson.JsonParseException;
  4. import org.codehaus.jackson.map.JsonMappingException;
  5. import org.codehaus.jackson.map.ObjectMapper;
  6. /**
  7. * JSON工具类
  8. * @author VIC
  9. *
  10. */
  11. public class JsonUtil {
  12. private static ObjectMapper mapper;
  13. static {
  14. mapper = new ObjectMapper();
  15. }
  16. /**
  17. * 1 对象转json
  18. * @param obj
  19. * @return
  20. */
  21. public static String pareseToJson(Object obj) throws JsonGenerationException, JsonMappingException, IOException{
  22. return mapper.writeValueAsString(obj) ;
  23. }
  24. /**
  25. * 2 json转class
  26. * @param json
  27. * @param classType
  28. * @return
  29. */
  30. public static <T> T parseToClass(String json, Class<T> classType) throws JsonParseException, JsonMappingException, IOException{
  31. return mapper.readValue(json, classType);
  32. }

2. 读取src下配置文件工具类

  1. import java.io.InputStream;
  2. import java.util.Properties;
  3. import org.apache.commons.lang.StringUtils;
  4. /**
  5. * 读取配置文件工具类
  6. * @author VIC
  7. *
  8. */
  9. public class PropertiesUtil {
  10. private static Properties properties = null;
  11. private static final String PROP_RESOURCE = "some.properties";
  12. public static Properties getPara(){
  13. properties = new Properties();
  14. try{
  15. InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(PROP_RESOURCE);
  16. properties.load(in);
  17. }catch(Exception e) {
  18. e.printStackTrace();
  19. }
  20. return properties;
  21. }
  22. public static String getStringByKey(String key){
  23. if(null == properties) {
  24. PropertiesUtil.getPara();
  25. }
  26. try{
  27. return properties.getProperty(key);
  28. }catch(Exception e) {
  29. e.printStackTrace();
  30. }
  31. return null;
  32. }
  33. public static int getIntByKey(String key ){
  34. String val = getStringByKey(key);
  35. if(StringUtils.isNotBlank(val)){
  36. return Integer.parseInt(val);
  37. }else {
  38. return 0;
  39. }
  40. }

3. 正则工具类

  1. import java.io.UnsupportedEncodingException;
  2. import java.net.URLEncoder;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import org.apache.commons.lang.StringUtils;
  9. /**
  10. * 正则工具类
  11. * @author Administrator
  12. *
  13. */
  14. public class RegexUtil {
  15. private static final String rootUrlRegex = "((http|https)://.*?/)";
  16. private static final String currentUrlRegex = "((http|https)://.*/)";
  17. private static final String CHRegex = "([\u4E00-\u9FA5])";
  18. /**
  19. * 1 返回字符串中匹配正则的字符串并根据分隔符拼接
  20. * @param srcStr 源字符串
  21. * @param regexStr 正则表达式
  22. * @param splitStr 分隔符
  23. * @param n 所在正则的位置
  24. * @return
  25. */
  26. public static String getString(String srcStr, String regexStr, String splitStr, int n){
  27. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || n < 1) {
  28. return "";
  29. }
  30. splitStr = splitStr == null ? "" : splitStr;
  31. //CASE_INSENSITIVE:当我们在匹配的时候要忽略字符大小写时 DOTALL:使用这个选项之后metacharacter .就可以包括一行的终止字符了,如果没有这个选项, 一行的终止字符,并不会考虑在字符串之内的。 使用这个选项会降低效率
  32. Pattern pattern = Pattern.compile(regexStr , Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  33. Matcher matcher = pattern.matcher(srcStr);
  34. StringBuffer sb = new StringBuffer();
  35. while( matcher.find() ){
  36. sb.append(matcher.group(n).trim()).append(splitStr);
  37. }
  38. if(sb.length() > 0) {
  39. sb.deleteCharAt(sb.length() - 1);
  40. }
  41. return sb.toString();
  42. }
  43. /**
  44. * 2 返回字符串中匹配正则的字符串拼接
  45. * @param srcStr
  46. * @param regexStr
  47. * @param n
  48. * @return
  49. */
  50. public static String getString(String srcStr, String regexStr, int n){
  51. return getString(srcStr, regexStr, null, n);
  52. }
  53. /**
  54. * 3 返回第一条匹配的结果
  55. * @param srcStr
  56. * @param regexStr
  57. * @param n
  58. * @return
  59. */
  60. public static String getFirstString(String srcStr, String regexStr, int n){
  61. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || n < 1) {
  62. return "";
  63. }
  64. Pattern pattern = Pattern.compile(regexStr , Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  65. Matcher matcher = pattern.matcher(srcStr);
  66. while( matcher.find()) {
  67. return matcher.group(n).trim();
  68. }
  69. return "";
  70. }
  71. /**
  72. * 4 回字符串中匹配正则的字符串 放入list集合
  73. * @param srcStr
  74. * @param regexStr
  75. * @param n
  76. * @return
  77. */
  78. public static List<String> getList(String srcStr, String regexStr, int n){
  79. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || n < 1) {
  80. return Collections.emptyList();
  81. }
  82. List<String> list = new ArrayList<>();
  83. Pattern pattern = Pattern.compile(regexStr , Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  84. Matcher matcher = pattern.matcher(srcStr);
  85. while( matcher.find() ){
  86. list.add(matcher.group(n));
  87. }
  88. return list;
  89. }
  90. /**
  91. * 5 获取字符中匹配正则的全部数据String集合 放入list集合
  92. * @param srcStr
  93. * @param regexStr
  94. * @param arr 正则位置数组
  95. * @return
  96. */
  97. public static List<String[]> getList(String srcStr, String regexStr, int[] arr){
  98. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || (arr == null || arr.length < 1)) {
  99. return Collections.emptyList();
  100. }
  101. int len = arr.length;
  102. for(int i=0; i< len; i++) {
  103. if(arr[i] < 1){
  104. return Collections.emptyList();
  105. }
  106. }
  107. List<String[]> list = new ArrayList<>();
  108. Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  109. Matcher matcher = pattern.matcher(srcStr);
  110. while ( matcher.find()) {
  111. String[] ss = new String[len];
  112. for(int i = 0; i<len; i++) {
  113. ss[i] = matcher.group(arr[i]).trim();
  114. }
  115. list.add(ss);
  116. }
  117. return list;
  118. }
  119. /**
  120. * 6 获取字符中匹配正则的全部数据String 放入list集合
  121. * @param srcStr
  122. * @param regexStr
  123. * @param arr 正则位置数组
  124. * @return
  125. */
  126. public static List<String> getStringList(String srcStr, String regexStr, int[] arr){
  127. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || (arr == null || arr.length < 1)) {
  128. return Collections.emptyList();
  129. }
  130. int len = arr.length;
  131. for(int i=0; i< len; i++) {
  132. if(arr[i] < 1){
  133. return Collections.emptyList();
  134. }
  135. }
  136. List<String> list = new ArrayList<>();
  137. Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  138. Matcher matcher = pattern.matcher(srcStr);
  139. while ( matcher.find()) {
  140. StringBuffer sb = new StringBuffer();
  141. for(int i = 0; i<len; i++) {
  142. sb.append(matcher.group(arr[i]).trim());
  143. }
  144. list.add(sb.toString());
  145. }
  146. return list;
  147. }
  148. /**
  149. * 7 获取字符中匹配正则的第一条数据 String[]
  150. * @param srcStr
  151. * @param regexStr
  152. * @param arr 正则位置数组
  153. * @return
  154. */
  155. public static String[] getFirstyArray(String srcStr, String regexStr, int[] arr){
  156. if(StringUtils.isEmpty(srcStr) || StringUtils.isEmpty(regexStr) || (arr == null || arr.length < 1)) {
  157. return null;
  158. }
  159. int len = arr.length;
  160. for (int i = 0; i < len; i++) {
  161. if (arr[i] < 1) {
  162. return null;
  163. }
  164. }
  165. Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  166. Matcher matcher = pattern.matcher(srcStr);
  167. while (matcher.find()) {
  168. String[] ss = new String[len];
  169. for (int i = 0; i < len; i++) {
  170. ss[i] = matcher.group(arr[i]).trim();
  171. }
  172. return ss;
  173. }
  174. return null;
  175. }
  176. /**
  177. * 8 .组装网址,网页的URL
  178. * @param url
  179. * @param currentUrl
  180. * @return
  181. */
  182. private static String getHttpUrl(String url, String currentUrl){
  183. try {
  184. //新增的replaceAll 转化有些地址接口中的转化地址,如: \/test\/1.html
  185. url = encodeUrlCh(url).replaceAll("\\\\/", "/");
  186. } catch (Exception e) {
  187. // TODO Auto-generated catch block
  188. e.printStackTrace();
  189. }
  190. if (url.indexOf("http") == 0){
  191. return url;
  192. }
  193. if (url.indexOf("/") == 0){
  194. return getFirstString(currentUrl, rootUrlRegex, 1) + url.substring(1);
  195. }
  196. if (url.indexOf("\\/") == 0){
  197. return getFirstString(currentUrl, rootUrlRegex, 1) + url.substring(2);
  198. }
  199. return getFirstString(currentUrl, currentUrlRegex, 1) + url;
  200. }
  201. /**
  202. * 9 获取和正则匹配的绝对链接地址
  203. * @param dealStr
  204. * @param regexStr
  205. * @param currentUrl
  206. * @param n
  207. * @return
  208. */
  209. public static List<String> getArrayList(String srcStr, String regexStr, String currentUrl, int n){
  210. List<String> reArrayList = new ArrayList<String>();
  211. if (srcStr == null || regexStr == null || n < 1 || srcStr.isEmpty()){
  212. return reArrayList;
  213. }
  214. Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  215. Matcher matcher = pattern.matcher(srcStr);
  216. while (matcher.find()) {
  217. reArrayList.add(getHttpUrl(matcher.group(n).trim(), currentUrl));
  218. }
  219. return reArrayList;
  220. }
  221. /**
  222. * 转化URL中的中文字符
  223. * @param url
  224. * @return
  225. * @throws UnsupportedEncodingException
  226. */
  227. public static String encodeUrlCh(String url) throws UnsupportedEncodingException {
  228. while(true){
  229. String s = getFirstString(url, CHRegex, 1);
  230. if(StringUtils.isEmpty(s)) {
  231. return url;
  232. }
  233. url = url.replace(s, URLEncoder.encode(s, "utf-8"));
  234. }
  235. }
  236. }

4. IOUtils

可结合commons-io 工具类使用

  1. /**
  2. * IO工具类
  3. * @author VIC
  4. *
  5. */
  6. public class IOUtils {
  7. /**
  8. * 1 根据路径和文件名创建文件
  9. * @param path
  10. * @param name
  11. * @return
  12. */
  13. public static File getFileByPathAndName(String path, String name){
  14. File dir = new File(path);
  15. if( !dir.exists() ){
  16. dir.mkdirs();
  17. }
  18. return new File(path, name);
  19. }
  20. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注