方法如下:
public static Map<String, String> fileToMap(String path){
Map<String,String> map = new HashMap<>();
File file = new File(path);
if (!file.exists()){
return null;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String temp = "";
int line = 1;
while ((temp = reader.readLine())!=null){
String[] arr = temp.split(":");
map.put(arr[0], arr[1]);
line++;
}
reader.close();
}catch (IOException e){
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return map;
}