public String getDeptOuId(String openId) throws IOException {
String deptId = "";
// 存储信息的文件的绝对路径
String csvPath = FileUtil.getWorkingPath()
.resolve(ConfigUtil.getConfig().get("idmappings")).toString();
System.out.print(csvPath);
//此处可以用工程中的相对路径
//String csvPath = "src/DeptInfo.csv";
try {
// 存储文件信息的Map
Map deptInfoMap = new HashMap();
// 读CSV文件
FileInputStream fileStream = new FileInputStream(csvPath);
//设置输出内容格式,防止乱码
InputStreamReader inputReader = new InputStreamReader(fileStream,"UTF-8");
BufferedReader reader =new BufferedReader(inputReader);
reader.readLine();
String line = "";
while ((line = reader.readLine()) != null) {
// 判断是否为注释
String item[] = line.split(",");// CSV格式文件为逗号分隔符文件,这里根据逗号切分
//将内容以键值的形式放到Map中
deptInfoMap.put(item[0], item[1]);
}
if (deptInfoMap != null) {
deptId = deptInfoMap.get(openId);
}
reader.close();
} catch (IOException ex) {
System.out.println("读写文件出错!");
}
return deptId;
}
原文:http://5fresh.blog.51cto.com/5472694/1433834