【特殊文件---properties】

properties 

1. 注释

在properties中注释是采用#号开头的方式来进行注释的

2. 编写properties文件

在properties中,一行就是一个键值对(key=value),简单的理解就是一行可以保存一个变量,键和值之间用=号隔开

记住,不用打双引号(“”)打了的话读取的时候会读取到(“”)除非你自己需要双引号
不要有重复的键,要不然后面的值会覆盖前面的值

3. 部分例子

# String
person.last-name=john
# int
person.age=112
# boolean
person.boss=false
# Date
person.birth=2019/11/12
# List<Object>
person.dogs[0].name=jj
person.dogs[0].age=111
person.dogs[1].name=tom
person.dogs[1].age=111
# Map<String, Object>
person.maps.Jack=jackc2
person.maps.Iris=yili8
# List
person.lists[0]=1
person.lists[1]=2

4. 注意事项

  • 避免重复键:在Properties文件中,每个键都应该是唯一的。如果出现重复的键,则后面的值将覆盖前面的值。
  • 编码:Properties文件默认使用ISO 8859-1(也称为Latin-1)编码。如果需要在文件中包含非ASCII字符,建议使用UTF-8编码,并在读取文件时指定正确的编码方式。
  • 文件扩展名:虽然Properties文件的扩展名不是强制的,但通常使用.properties作为文件扩展名,以便于识别和管理。

使用Properties读取属性文件里的键值对数据

构造函数

Properties()

这个构造函数用于创建一个空的Properties对象,即不包含任何键值对的集合。

Properties properties = new Properties();  
// 现在properties是一个空的Properties对象

Properties(String fileName)

这个构造函数尝试从指定的文件名加载属性列表(key 和 value 对)。

public class Main {
    public static void main(String[] args) throws Exception {
        Properties properties=new Properties();
        System.out.println(properties);
        System.out.println("---------------------------");
        //这里他担心你文件不存在。所以你只需要alt加enter键抛出异常就可以了。
        properties.load(new FileReader("E:\\code\\javese\\summerVacation\\src\\love.properties"));
        System.out.println(properties);
    }
}

常用方法

public void load(InputStream is)

通过字节输入流读取属性文件(如.properties文件)中的键值对数据。

try (FileInputStream fis = new FileInputStream("config.properties")) {  
    Properties properties = new Properties();  
    properties.load(fis);  
    // 处理properties中的数据  
} catch (IOException e) {  
    e.printStackTrace();  
}

public void load(Reader reader)

通过字符输入流读取属性文件中的键值对数据。这适用于需要处理非二进制属性文件(如使用特定字符编码的文件)的情况。

try (FileReader fr = new FileReader("config.properties");  
     BufferedReader br = new BufferedReader(fr)) {  
    Properties properties = new Properties();  
    properties.load(br);  
    // 处理properties中的数据  
} catch (IOException e) {  
    e.printStackTrace();  
}

public String getProperty(String key)

根据指定的键获取对应的值。如果找不到该键,则返回null

Properties properties = // 假设这里已经加载了属性文件  
String databaseUrl = properties.getProperty("database.url");  
System.out.println("Database URL: " + databaseUrl);

public Set<String> stringPropertyNames()

返回此属性列表中所有键的Set视图,其中键及其对应的值是字符串。这相当于调用了keySet()方法,但返回的是Set<String>而不是Set<Object>,因为Properties中的键和值都是字符串。

Properties properties = // 假设这里已经加载了属性文件  
Set<String> keys = properties.stringPropertyNames();  
for (String key : keys) {  
    System.out.println(key + ": " + properties.getProperty(key));  
}

将键值对数据写入属性文件的Properties类常用方法

setProperty(String key, String value)

这个方法用于在Properties对象中设置或更新一个键值对。它接受两个参数:key(键)和value(值),两者都是字符串类型。如果键已经存在,则该方法会替换原有的值;如果键不存在,则会在Properties对象中添加一个新的键值对。

示例代码

Properties props = new Properties();  
props.setProperty("database.url", "jdbc:mysql://localhost:3306/mydb");  
props.setProperty("username", "admin");  
// 现在props对象中包含了两个键值对

store(OutputStream os, String comments)

这个方法用于将Properties对象中的键值对写入到指定的输出流中,通常是文件输出流(FileOutputStream),从而生成一个属性文件。它接受两个参数:os(输出流)和comments(注释)。注释字符串将作为属性文件的头部注释写入文件,如果为null,则不写入注释。

示例代码

Properties props = // 假设这里已经设置了键值对  
try (FileOutputStream fos = new FileOutputStream("config.properties")) {  
    props.store(fos, "This is a configuration file for my application.");  
    // 现在config.properties文件包含了props对象中的所有键值对,以及一个头部注释  
} catch (IOException e) {  
    e.printStackTrace();  
}

store(Writer w, String comments)

这个方法与store(OutputStream os, String comments)类似,但它接受的是一个Writer对象而不是OutputStream。这允许你以字符为单位写入数据,这对于需要特定字符编码的属性文件特别有用。同样,它也接受一个注释字符串作为第二个参数。

示例代码

Properties props = // 假设这里已经设置了键值对  
try (FileWriter fw = new FileWriter("config_utf8.properties", StandardCharsets.UTF_8)) {  
    props.store(fw, "This is a UTF-8 encoded configuration file.");  
    // 现在config_utf8.properties文件包含了props对象中的所有键值对,以及一个头部注释,并且文件以UTF-8编码保存  
} catch (IOException e) {  
    e.printStackTrace();  
}

注意事项

  • 在使用store方法时,请确保你有权写入指定的文件路径,并且该文件路径是有效的。
  • 如果文件已经存在,store方法会覆盖它。如果你不想覆盖现有文件,请在写入之前进行检查。
  • 注释字符串是可选的,但如果你提供了它,它将被写入属性文件的开头,作为头部注释。
  • 当处理完文件后,确保关闭输出流或写入器,以避免资源泄露。在上面的示例中,我使用了try-with-resources语句来自动管理资源。
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值