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;
}
}
}
将HashMap,HashSet写入文本的工具类
最新推荐文章于 2024-02-22 15:18:28 发布