阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用Properties

26次阅读
没有评论

共计 3062 个字符,预计需要花费 8 分钟才能阅读完成。

在编写应用程序的时候,经常需要读写配置文件。例如,用户的设置:

# 上次最后打开的文件:
last_open_file=/data/hello.txt
# 自动保存文件的时间间隔:
auto_save_interval=60

配置文件的特点是,它的 Key-Value 一般都是 StringString 类型的,因此我们完全可以用 Map<String, String> 来表示它。

因为配置文件非常常用,所以 Java 集合库提供了一个 Properties 来表示一组“配置”。由于历史遗留原因,Properties内部本质上是一个 Hashtable,但我们只需要用到Properties 自身关于读写配置的接口。

读取配置文件

Properties 读取配置文件非常简单。Java 默认配置文件以 .properties 为扩展名,每行以 key=value 表示,以 # 课开头的是注释。以下是一个典型的配置文件:

# setting.properties

last_open_file=/data/hello.txt
auto_save_interval=60

可以从文件系统读取这个 .properties 文件:

String f = "setting.properties";
Properties props = new Properties();
props.load(new java.io.FileInputStream(f));

String filepath = props.getProperty("last_open_file");
String interval = props.getProperty("auto_save_interval", "120");

可见,用 Properties 读取配置文件,一共有三步:

  1. 创建 Properties 实例;
  2. 调用 load() 读取文件;
  3. 调用 getProperty() 获取配置。

调用 getProperty() 获取配置时,如果 key 不存在,将返回null。我们还可以提供一个默认值,这样,当 key 不存在的时候,就返回默认值。

也可以从 classpath 读取 .properties 文件,因为 load(InputStream) 方法接收一个 InputStream 实例,表示一个字节流,它不一定是文件流,也可以是从 jar 包中读取的资源流:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/common/setting.properties"));

试试从内存读取一个字节流:

// properties
import java.io.*;
import java.util.Properties;

public class Main {public static void main(String[] args) throws IOException {String settings = "# test" + "\n" + "course=Java" + "\n" + "last_open_date=2019-08-07T12:35:01";
        ByteArrayInputStream input = new ByteArrayInputStream(settings.getBytes("UTF-8"));
        Properties props = new Properties();
        props.load(input);

        System.out.println("course:" + props.getProperty("course"));
        System.out.println("last_open_date:" + props.getProperty("last_open_date"));
        System.out.println("last_open_file:" + props.getProperty("last_open_file"));
        System.out.println("auto_save:" + props.getProperty("auto_save", "60"));
    }
}

如果有多个 .properties 文件,可以反复调用 load() 读取,后读取的 key-value 会覆盖已读取的 key-value:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/common/setting.properties"));
props.load(new FileInputStream("C:\\conf\\setting.properties"));

上面的代码演示了 Properties 的一个常用用法:可以把默认配置文件放到 classpath 中,然后,根据机器的环境编写另一个配置文件,覆盖某些默认的配置。

Properties设计的目的是存储 String 类型的 key-value,但 Properties 实际上是从 Hashtable 派生的,它的设计实际上是有问题的,但是为了保持兼容性,现在已经没法修改了。除了 getProperty()setProperty()方法外,还有从 Hashtable 继承下来的 get()put()方法,这些方法的参数签名是 Object,我们在使用Properties 的时候,不要去调用这些从 Hashtable 继承下来的方法。

写入配置文件

如果通过 setProperty() 修改了 Properties 实例,可以把配置写入文件,以便下次启动时获得最新配置。写入配置文件使用 store() 方法:

Properties props = new Properties();
props.setProperty("url", "http://www.liaoxuefeng.com");
props.setProperty("language", "Java");
props.store(new FileOutputStream("C:\\conf\\setting.properties"), "这是写入的 properties 注释");

编码

早期版本的 Java 规定 .properties 文件编码是 ASCII 编码(ISO8859-1),如果涉及到中文就必须用 name=\u4e2d\u6587 来表示,非常别扭。从 JDK9 开始,Java 的 .properties 文件可以使用 UTF- 8 编码了。

不过,需要注意的是,由于 load(InputStream) 默认总是以 ASCII 编码读取字节流,所以会导致读到乱码。我们需要用另一个重载方法 load(Reader) 读取:

Properties props = new Properties();
props.load(new FileReader("settings.properties", StandardCharsets.UTF_8));

就可以正常读取中文。InputStreamReader 的区别是一个是字节流,一个是字符流。字符流在内存中已经以 char 类型表示了,不涉及编码问题。

小结

Java 集合库提供的 Properties 用于读写配置文件 .properties.properties 文件可以使用 UTF- 8 编码;

可以从文件系统、classpath 或其他任何地方读取 .properties 文件;

读写 Properties 时,注意仅使用 getProperty()setProperty()方法,不要调用继承而来的 get()put()等方法。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-08-05发表,共计3062字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中