【常见配置文件】Property 文件(Java)

Property文件定义

属性文件(Property file)是一种常用的配置文件格式,它通常包含键值对,用于存储应用程序的配置信息。 属性文件以.properties为扩展名,可以使用任何文本编辑器来创建和编辑。

常见数据库配置: 属性文件中每个键值对都以“键=值”的形式表示,其中键和值都是字符串,用等号连接。例如:

database.url=jdbc:mysql://localhost:3306/mydb
database.username=root
database.password=password123
复制代码

某个对象里的属性使用 ·号 来获取,并用=来给它赋值。

这样的配置文件还是比较简单的,经常来Java的springboot文件配置可以看到

Java读取Property文件

Java包里有专门处理property文件的包 java.util.Properties;

配置文件property

database.data = 500
database.name = learnOnWeb
复制代码

Main文件

public class Main {
    public static void main(String[] args) {
        // 初始化Property对象
        // new之后的Property文件是一个拥有map集合的对象,之后我们需要的键值对就放在该map里面
        Properties props = new Properties();
        try {
            // 加载配置文件
            // 我用绝对路径读不到,但是用绝对路径可以,如果大家读取失败可以试试全局路径     
            props.load(new FileInputStream("D:\works\untitled2\src\main\java\property\property.properties"));
            // 调用getProperty去获取属性值
            String name = props.getProperty("database.name");
            String data = props.getProperty("database.data");
            System.out.println("name: " + name);
            System.out.println("data: " + data);
            // 读取文件之后可以进行操作了,比如通过反射来生成类。
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

Java修改XML文件

修改文件也不难,使用setProperty去设置就好了,然后将其输出到文件中(调用store函数,第一个参数为输出流,第二个参数为注释的内容)

public class Main {
    public static void main(String[] args) throws IOException {
        // 创建一个Properties对象并加载文件
        Properties props = new Properties();
        FileInputStream in = new FileInputStream("D:\works\untitled2\src\main\java\property\property.properties");
        props.load(in);
        in.close();

        // 修改属性值
        props.setProperty("database.data", "100");

        // 添加新属性
        props.setProperty("database.url", "localhost");

        // 保存修改后的属性到文件中
        FileOutputStream out = new FileOutputStream("D:\works\untitled2\src\main\java\property\after" +
                ".properties");
        // 第二个参数时为修改添加注释
        props.store(out, "Updated properties");
        out.close();
    }
}
复制代码

小贴士

当property文件中拥有非ISO-8859-1码的字符时,我们可以选择使用UTF-8 去读取。但是FileInputStream流是按字节读取的,因此我们要换一种编码方式的话,需要使用装饰类InputStreamReader去装饰,使用如下:

FileInputStream读取配置文件将其转化为字节数组后,我们使用 new InputStreamReader(将字节流转化为字符流)去读取,这个类可以去指定编码方式UTF-8或其它,这样我们在读取字符时可以防止乱码。

又出现一个问题: 我们的Property文件是ISO-8859-1标准编码, 并不能读取中文,我们要读取中文可以手动将该配置文件的编码方式换为UTF-8

比如: 当配置文件properties文件中含有中文时,改成这样就好了

这里是配置文件的内容

database.name=我是中国人
database.data=500
复制代码

主类:

public class Main {
    public static void main(String[] args) throws IOException {
        // 创建一个Properties对象并加载文件
        Properties props = new Properties();
        FileInputStream in = new FileInputStream("D:\works\untitled2\src\main\java\property\property.properties");
        props.load(new InputStreamReader(in, "UTF-8"));
        in.close();

        // 修改属性值
        props.setProperty("database.name", "倒萨倒萨");

        // 添加新属性
        props.setProperty("database.url", "localhost");

        // 保存修改后的属性到文件中
        FileOutputStream out = new FileOutputStream("D:\works\untitled2\src\main\java\property\after" +
                ".properties");
        props.store(new OutputStreamWriter(out, "UTF-8"), "Updated properties");
        out.close();
    }
}

作者:按时交作业
链接:https://juejin.cn/post/7223790746615873596
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SpringBoot中,可以通过使用@ConfigurationProperties注解来读取配置文件中的属性值。为了保护敏感信息(如密码),可以对读取到的属性值进行脱敏处理。 一种常见的脱敏处理方式是对字符串进行部分替换,只保留前几位和后几位,中间用*代替。下面是一个例子: ```java @Component @ConfigurationProperties(prefix = "myconfig") public class MyConfig { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @PostConstruct public void init() { // 对密码进行脱敏处理 if (StringUtils.isNotBlank(password)) { int len = password.length(); if (len <= 2) { password = "**"; } else if (len <= 4) { password = password.charAt(0) + "**" + password.charAt(len - 1); } else if (len <= 6) { password = password.charAt(0) + "**" + password.substring(len - 3, len - 1) + password.charAt(len - 1); } else { password = password.charAt(0) + "**" + password.substring(len - 5, len - 1) + password.charAt(len - 1); } } } } ``` 在上面的例子中,我们定义了一个MyConfig类,使用@ConfigurationProperties注解读取配置文件中的属性值,并对密码进行脱敏处理。在init()方法中,我们使用StringUtils类的isNotBlank()方法判断密码是否为空,然后根据密码的长度进行不同的脱敏处理。 需要注意的是,我们对密码进行了修改,但是并没有修改配置文件中的值。如果需要修改配置文件中的值,可以使用Spring的Environment对象进行修改。下面是一个例子: ```java @Autowired private Environment environment; @PostConstruct public void init() { // 对密码进行脱敏处理 if (StringUtils.isNotBlank(password)) { int len = password.length(); if (len <= 2) { password = "**"; } else if (len <= 4) { password = password.charAt(0) + "**" + password.charAt(len - 1); } else if (len <= 6) { password = password.charAt(0) + "**" + password.substring(len - 3, len - 1) + password.charAt(len - 1); } else { password = password.charAt(0) + "**" + password.substring(len - 5, len - 1) + password.charAt(len - 1); } // 修改配置文件中的值 environment.getPropertySources().forEach(propertySource -> { if (propertySource instanceof EnumerablePropertySource) { String[] propertyNames = ((EnumerablePropertySource) propertySource).getPropertyNames(); for (String propertyName : propertyNames) { Object value = propertySource.getProperty(propertyName); if (StringUtils.isNotBlank(value.toString()) && value.toString().equals(password)) { ((StandardEnvironment) environment).getPropertySources() .addFirst(new MapPropertySource("myConfig", Collections.singletonMap(propertyName, password))); } } } }); } } ``` 在这个例子中,我们注入了Environment对象,并在init()方法中使用它修改了配置文件中的值。需要注意的是,修改配置文件中的值可能会影响程序的正常运行,因此要慎重考虑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值