在Java中,可以通过Properties类来读取和写入属性文件。下面是Properties类的使用示例:
Properties properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("config.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
String value = properties.getProperty("key");
System.out.println(value);
properties.setProperty("key", "value");
try {
FileOutputStream fileOutputStream = new FileOutputStream("config.properties");
properties.store(fileOutputStream, "Config Properties");
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
以上就是使用Properties类读取和写入属性文件的基本步骤。需要注意的是,属性文件的格式为键值对,如"key=value",每个属性占一行。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>