通过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"));
 
    }
posted @   南翔技校毕业后  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示