前言
回顾这一年,我的付出终归是没有白费的!很快,今天是2021年了,激烈的金三银四已经没有多少日子就有上场了!所以今天,我挑选了“美团+字节+腾讯”等三家的一二三面问题,在此篇文章里做一个分享,希望看到的朋友可以做一个面试参考,并可自行测挑战一下你能走到哪一面,期待你的表现~
Properties:

中文:
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
英文:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.
这里简单列举几个常用的方法,接下来的操作我也将会用到这些方法;
1,String getProperty(String key)
简介:通过key获取value


2,void load(InputStream inStream/Reader reader)
简介:将文件内容读入内存


3,Object setProperty(String key, String value)
简介:设置(增加)新的键值对


4,void store(OutputStream out / Writer writer, String comments)
简介:将内存中对应内容写入文件


这几个方法是比较常用的,当然还有几个方法没有写,如果需要可以在API文档中查询;
接下来我将会用代码实现一些操作,在这里先把.properties文件放出来:
下面的代码操作都是基于这个文件;

读取操作可以通过load方法读取文件,再通过getProperty获取value值;
下面我用一个代码示范:
package io;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
// 实现对.properties 文件的读取
public class PropertiesBlog01 {
// 读入.properties文件
public void readProperties() throws IOException {
Properties properties = new Properties();
// 把文件的数据读入内存
properties.load(new FileInputStream(“blogtest.properties”));
// 输出文件的内容
System.out.println(properties.getProperty(“name”)); // 获取姓名
System.out.println(properties.getProperty(“age”)); // 获取年龄
System.out.println(properties.getProperty(“gender”)); // 获取性别
}
// main方法调用测试
public static void main(String[] args) throws IOException {
PropertiesBlog01 propertiesBlog01 = new PropertiesBlog01();
propertiesBlog01.readProperties();
}
}
输出结果:
jack
18
man
写入操作就有点意思了,这里写入分为三种情况:
-
写入新数据覆盖之前的数据
-
写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
-
写入新数据不会覆盖之前的数据,且有重复的键值会覆盖
这三种情况一般按需求使用,但是我感觉最常用的还是第三种(只是个人观点);
下面我用代码示范一下,代码中已有详细注释;
package io;
import java.io.*;
import java.util.Properties;
// 实现对.properties 文件的写入
public class PropertiesBlog02 {
// 写入.properties 文件
public void writeProperties() throws IOException {
Properties properties = new Properties();
// 下面是三种写入情况
// 1,写入新数据覆盖之前的数据
// 设置文件增添内容
properties.setProperty(“IdNum”, “123456789”);
properties.store(new FileOutputStream(“blogtest.properties”), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据覆盖后为:
// name=jack #this is a comment
// age=18 #Fri Nov 19 19:58:08 CST 2021
// gender=man IdNum=123456789
// 2,写入新数据不会覆盖之前的数据,但是如果有重复键值不会覆盖
// 设置文件增添内容
properties.setProperty(“IdNum”, “repeat”);
properties.store(new FileOutputStream(“blogtest.properties”, true), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据后为:
// #this is a comment #this is a comment
// #Fri Nov 19 19:58:08 CST 2021 #Fri Nov 19 20:06:03 CST 2021
// IdNum=123456789 IdNum=123456789
// #this is a comment
// #Fri Nov 19 20:06:03 CST 2021
// IdNum=repeat
// 3,写入新数据不会覆盖之前的数据,且有重复的键值会覆盖
properties.load(new FileInputStream(“blogtest.properties”));
properties.setProperty(“IdNum”, “newIdNum”);
properties.store(new FileOutputStream(“blogtest.properties”), “this is a comment”);
// 输出结果:
// 原文件内容: 写入新数据后为:
// #this is a comment #this is a comment
// #Fri Nov 19 20:06:03 CST 2021 #Fri Nov 19 20:13:22 CST 2021
// IdNum=123456789 IdNum=newIdNum
// #this is a comment
// #Fri Nov 19 20:06:03 CST 2021
// IdNum=repeat
}
public static void main(String[] args) throws IOException {
PropertiesBlog02 propertiesBlog02 = new PropertiesBlog02();
propertiesBlog02.writeProperties();
}
}
本文介绍了Java中的Properties类,包括其用途、常用方法(如getProperty、load、setProperty、store等),并通过示例演示了如何进行文件的读取和写入操作,涉及覆盖和默认值策略。

被折叠的 条评论
为什么被折叠?



