Java加载jar包外的配置文件,转为map获取参数

某些时候我们需要将配置文件外置,放在jar包外方便修改

位置如图所示

Java加载jar包外的配置文件,转为map获取参数

  @Test
    public void ceshi() throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream("cap.properties");
        try {
            byte[] b = new byte[inputStream.available()];//新建一个字节数组
            inputStream.read(b);//将文件中的内容读取到字节数组中
            inputStream.close();
            String str2 = new String(b);//再将字节数组中的内容转化成字符串形式输出
            Map<String, String> map = Splitter.on('\n')
                    .trimResults()
                    .withKeyValueSeparator("=")
                    .split(str2);
            System.out.println(map);
            System.out.println(map.get("userName"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

上述是我写的一个测试代码,你们可以抽出来写个工具类

上一篇:Java读取文本文件


下一篇:java基础之InputStream的使用