java中的properties是什么?
Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,能让用户够脱离程序本身去修改相关的变量设置。就像在Java中,其配置文件常为.properties文件,是以键值对的形式进行参数配置的。
创建一个config.properties文件(我添加到的是resources的目录下)
添加内容:
type.1=我是测试1
type.2=我是测试2
type.3=我是测试3
编写一个获取值的工具类Properties_Sun_Util
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Properties_Sun_Util {
/**
* @Description: config.properties名的专属方法
* key:输入键,得到值
* @Date: 2019/11/11 9:39
* @Param:
*/
public static String config(String key) {
String value = "";
Properties properties = new Properties();
try {
properties = PropertiesLoaderUtils.loadAllProperties("config.properties");
value = new String(properties.getProperty(key).getBytes("iso-8859-1"), "gbk");
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
}
测试(方法传入不同的键获取对应的值):
public static void main(String[] args) {
System.out.println(config("type.1"));
}
结果:
才疏学浅 ,请多指教 !
参考:https://blog.csdn.net/liuyunyihao/article/details/81229009