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);
    }
*/
复制代码

这个工具类太定制化了,而且判断做的不好,就是简单的写一下,仅供参考

posted @   potent_prince  阅读(602)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示