标签:imp hashmap try read prope value input article isp
下边是整理的一些Java开发的utils,顺便吐槽下新浪博客的编辑器排版跟我写的博客一样 烂,所以采用的博客园
1 public static SimpleDateFormat df1 = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US); 2 public static SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); 3 df2.format(df1.parse(time_local));
1 try { 2 int i = Integer.parseInt(str); 3 } catch (NumberFormatException e) { //str中可能有非数字 4 e.printStackTrace(); 5 }
1 String formatted = String.format("%s今年%d岁。", "我", 25); // 打印结果:"我今年25岁。"
1 @JSONField(name="role_name") 2 private String roleName;
public class A { // 配置date序列化和反序列使用yyyyMMdd日期格式 @JSONField(format="yyyyMMdd") public Date date; }
1 public static class VO { 2 @JSONField(ordinal = 3) 3 private int f0; 4 5 @JSONField(ordinal = 2) 6 private int f1; 7 8 @JSONField(ordinal = 1) 9 private int f2;
1 public class A { 2 @JSONField(serialize=false) 3 public Date date; 4 }
1 //获得指定数目的UUID 2 public static String[] getUUID(int number){ 3 if(number < 1){ 4 return null; 5 } 6 String[] retArray = new String[number]; 7 for(int i=0;i){ 8 retArray[i] = getUUID(); 9 } 10 return retArray; 11 } 12 //获得一个UUID 13 public static String getUUID(){ 14 String uuid = UUID.randomUUID().toString(); 15 //去掉“-”符号 16 return uuid.replaceAll("-", ""); 17 }
具有封装的读写文件、复制文件等功能。例如:
1 import org.apache.commons.io.FileUtils; 2 List lines = new ArrayList(); 3 ... 4 FileUtils.writeLines(new File("/Users/admin/1.txt"), lines, true); 5 String result = FileUtils.readFileToString(new File("/Users/admin/1.txt"), "UTF-8");
一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里。在程序中就可以直接读取了,例如:
1 InputStream input =Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.properties"); 2 Properties prop = new Properties(); 3 prop.load(input);
1 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(yamlPath); 2 Yaml yaml = new Yaml(); 3 HashMap map = yaml.loadAs(inputStream, HashMap.class);
标签:imp hashmap try read prope value input article isp
原文地址:https://www.cnblogs.com/lcmichelle/p/10742615.html