通过key,获取.properties文件中的值
news.properties文件:
#title
titleNode=/body/div[5]/div[2]/div[3]/h1/span[1]
#theYear
theYearNode=/body/div[5]/div[2]/div[1]/span[1]/em
#monthAndDay
monthAndDayNode=/body/div[5]/div[2]/div[1]/span[2]
#theTime
theTimeNode=/body/div[5]/div[2]/div[1]/span[3]
#source
sourceNode=/body/div[5]/div[2]/div[2]
#content
contentNode=//*[@id=\"detail\"]
#author
authorNode=//*[@id=\"articleEdit\"]/span[2]
#source的正则表达式
sourceRegex=来源:(.*)
#数据库连接
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://10.10.50.160:3306/learningspider?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username=root
password=abc123456#
代码实现
/**
* @author jiangl
* @ClassName loadProperty.java
* @Description 加载news.properties配置文件
* @createTime 2021年01月11日 21:24
*/
public class loadProperty
{
//定义properties配置文件路径
final static String filePath = "src/main/resources/news.properties";
/*
通过key,获取properties的值,已经处理中文乱码
*/
public static String load(String key) throws IOException
{
Properties properties = new Properties();
InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "GBK");
BufferedReader read = new BufferedReader(isr);
properties.load(read);
return properties.getProperty(key);
}
}
测试
@Test
public void test() throws IOException
{
System.out.println(load("sourceRegex"));
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南