java读取项目中的.ini或者.TXT 文件,返回集合
一段代码,可参考
public class ReadTxtUtil { /** * 读取内容 */ public HashMap<String, String> read(String filePath) { BufferedReader br = null; String line = null; try { //根据文件路径创建缓冲输入流 br = new BufferedReader(new FileReader(filePath));//filePath中是aaa.txt文件 String str = ""; HashMap<String, String> otherHashMap = new HashMap<>(16); //循环读取文件的每一行,对需要修改的行进行修改,放入缓冲对象中 while ((line = br.readLine()) != null) { //设置正则将多余空格都转为一个空格 str = line + "\r\n"; String[] dictionary = str.split("\\s{2,}|\t"); if (dictionary.length != 0) { String[] splits = dictionary[0].split(" "); //除了pz.ini文件其他的文件的处理格式 if (splits.length >= 2) { if (splits[0].contains("_")) { String[] ortherSplit = splits[0].split("_"); if (ortherSplit.length >= 3) { otherHashMap.put(ortherSplit[0], ortherSplit[2]); } } else { otherHashMap.put(splits[0], splits[1]); } } } } return otherHashMap; } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 if (br != null) { try { br.close(); } catch (IOException e) { br = null; } } } return null; } /* public static void main(String[] args) { String filePath = "D:/project/datacapture/datacapture-biz/src/main/java/com/internet/data/capture/util/HBGL.ini"; // 文件路径 read(filePath); } */
这个工具类太定制化了,而且判断做的不好,就是简单的写一下,仅供参考
想要体面生活,又觉得打拼辛苦;想要健康身体,又无法坚持运动。人最失败的,莫过于对自己不负责任,连答应自己的事都办不到,又何必抱怨这个世界都和你作对?人生的道理很简单,你想要什么,就去付出足够的努力。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步