将HashMap,HashSet写入文本的工具类
来源:互联网 发布:韩国美女直播秀软件 编辑:程序博客网 时间:2024/05/02 02:42
public class HashMapTools { public static void saveHashSet(HashSet<String> h, String fileName) { String[] o = h.toArray(new String[h.size()]); try { File f = new File(fileName); BufferedWriter output = new BufferedWriter(new FileWriter(f)); for (int i = 0; i < h.size(); i++) { output.write(o[i] + "\r\n"); } output.close(); } catch (Exception e) { e.printStackTrace(); } } public static void loadHashSet(HashSet<String> h, String fileName) { BufferedReader in; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { if (!h.contains(line)) h.add(line); } } catch (Exception e) { e.printStackTrace(); } } public static void saveHashMap(HashMap<String,String> h, String fileName) { try { File f = new File(fileName); BufferedWriter output = new BufferedWriter(new FileWriter(f)); for( String key: h.keySet()){ output.write(key + "," + h.get(key) + "\r\n"); } output.close(); } catch (Exception e) { e.printStackTrace(); } } public static void loadHashMap(HashMap<String,String> h, String fileName) { h.clear(); BufferedReader in; String[] keyAndValue = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { keyAndValue = line.split(","); if (!h.containsKey(keyAndValue[0])) h.put(keyAndValue[0], keyAndValue[1]); } } catch (Exception e) { e.printStackTrace(); } } public static void loadSIHashMap(HashMap<String,Integer> h, String fileName) { h.clear(); BufferedReader in; String[] keyAndValue = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { keyAndValue = line.split(","); if (!h.containsKey(keyAndValue[0])) h.put(keyAndValue[0], Integer.valueOf(keyAndValue[1])); } } catch (Exception e) { e.printStackTrace(); } } public static HashMap<String,Integer> loadSIHashMap( String fileName) { HashMap<String,Integer> h = new HashMap<String,Integer>(); BufferedReader in; String[] keyAndValue = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { keyAndValue = line.split(","); if (!h.containsKey(keyAndValue[0])) h.put(keyAndValue[0], Integer.valueOf(keyAndValue[1])); } } catch (Exception e) { e.printStackTrace(); } return h; } public static void saveSIHashMap(HashMap<String,Integer> h, String fileName) { try { File f = new File(fileName); BufferedWriter output = new BufferedWriter(new FileWriter(f)); for( String key: h.keySet()){ output.write(key + "," + h.get(key).toString() + "\r\n"); } output.close(); } catch (Exception e) { e.printStackTrace(); } } public static void loadIIHashMap(HashMap<Integer,Integer> h, String fileName) { h.clear(); BufferedReader in; String[] keyAndValue = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { keyAndValue = line.split(","); if (!h.containsKey(keyAndValue[0])) h.put( parserInt( keyAndValue[0]), parserInt(keyAndValue[1])); } } catch (Exception e) { e.printStackTrace(); } } public static void saveIIHashMap(HashMap<Integer,Integer> h, String fileName) { try { File f = new File(fileName); BufferedWriter output = new BufferedWriter(new FileWriter(f)); for( Integer key: h.keySet()){ output.write(key + "," + h.get(key) + "\r\n"); } output.close(); } catch (Exception e) { e.printStackTrace(); } } public static void loadLIHashMap(HashMap<Long,Integer> h, String fileName) { h.clear(); BufferedReader in; String[] keyAndValue = null; try { in = new BufferedReader(new InputStreamReader(new FileInputStream( fileName), "gbk")); String line; while ((line = in.readLine()) != null) { keyAndValue = line.split(","); if (!h.containsKey(keyAndValue[0])) h.put( parserLong( keyAndValue[0]), parserInt(keyAndValue[1])); } } catch (Exception e) { e.printStackTrace(); } } public static void saveLIHashMap(HashMap<Long,Integer> h, String fileName) { try { File f = new File(fileName); BufferedWriter output = new BufferedWriter(new FileWriter(f)); for( Long key: h.keySet()){ output.write(key + "," + h.get(key) + "\r\n"); } output.close(); } catch (Exception e) { e.printStackTrace(); } } public static int parserInt( String str ){ try { return Integer.parseInt(str); } catch (NumberFormatException e) { return 0; } } public static long parserLong( String str ){ try { return Long.parseLong(str); } catch (NumberFormatException e) { return 0l; } }}
0 0
- 将HashMap,HashSet写入文本的工具类
- 将文本写入excel的vbs
- hashset,hashmap的区别
- Java的HashMap,HashSet
- JCF map遍历 hashSet treeSet hashMap treeMap Collections工具类
- 读取文本的内容和将文本写入文件中
- HashMap、HashSet、HashTable的区别
- HashSet HashTable HashMap的区别
- HashSet和HashMap的关系
- HashSet和HashMap的关系
- hashmap、hashset、hashtable的关系
- HashSet和HashMap的关系
- HashSet HashTable HashMap的区别
- HashSet与HashMap的区别
- HashSet与HashMap的区别
- HashMap和HashSet的区别
- HashSet和HashMap的区别
- HashSet和HashMap的区别
- Android 6.0 运行时权限处理
- 贪心1008
- Hibernate 集合属性的操作
- spark HA
- hibernate 数据库 方言配置在哪个jar包的什么位置?
- 将HashMap,HashSet写入文本的工具类
- 自动异机(异地)备份
- word中实现章节标题的自动编号
- 深入探索Java 8 Lambda表达式
- 了解基于Token的身份验证的来龙去脉
- live555断流
- 求平面上N点最远两点和最近两点距离
- opencv特征检测出现Unhandled exception at……Access violation reading location 0x00000000.
- SQL语句的一些基础操作