存儲一些代碼片段哈 有些東西時間一久就忘記了,於是遍記錄下來,以方便查閱。
持續修改更新中....
獲取系統默認編碼: 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();
|