存储一些代码片段哈 有些东西时间一久就忘记了,于是遍记录下来,以方便查阅。
持续修改更新中....
获取系统默认编码: System.getProperty("file.encoding"); Charset.defaultCharset();也是可以的 |
获取当前程序运行的路径,很巧妙 HOHO File directory = new File("."); directory.getCanonicalPath(); thanks to http://blog.csdn.net/hxirui/article/details/514575 |
hashtable的遍历方法: 第一种: for(Iterator itr = ht.keySet().iterator(); itr.hasNext();){ String key = (String) itr.next(); String value = (String) ht.get(key); System.out.println(key+"--"+value); } 第二种: Enumeration e1 = ht.elements(); while (e1.hasMoreElements()) { System.out.println(e1.nextElement()); } 第三种: Enumeration e2 = ht.keys(); while (e2.hasMoreElements()) { String key = (String) e2.nextElement(); System.out.println(key +"---"+ht.get(key)); } thanks to http://blog.csdn.net/onisland/article/details/5609762 |
比较器的生成与使用:关键在于这句:{(x, y) such that c.compare(x, y) <= 0}. Comparator<String> comparator = new Comparator<String>(){ @Override public int compare(String o1, String o2) { int countO1 = word_count.get(o1); int countO2 = word_count.get(o2); if(countO1 > countO2){ return -1; }else if(countO1 < countO2){ return 1; }else{ return 0; } } }; Collections.sort(sortList,comparator); |
json的基本操作备份 JSONObject rootObj = JSONObject.fromObject(jsonStr); JSONObject dataObj = rootObj.getJSONObject("data"); JSONArray infoArray = dataObj.getJSONArray("info"); for (int i = 0; i < infoArray.size(); ++i) { JSONObject tmpObj = infoArray.getJSONObject(i); } |
java按行读取文本文件 BufferedReader reader = new BufferedReader(new FileReader(_fileAdr)); String line = reader.readLine(); while(line != null){ System.out.println(line); line = reader.readLine(); } reader.close();
|