将HashMap,HashSet写入文本的工具类

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;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值