java yml_java配置文件properties,yml,一般文件

JAVA编写配置文件的几种方式:

JAVA配置文件,一般都放在resource目录下,无论是下面提到的properties、yml还是普通的txt等文件。

在打成jar包之后,只需要jar包程序就可运行,如果要修改配置文件,只需将配置文件放在与jar包同一目录下即可,jar包会自动读取。

5bb4e0cc0973624968aa3ba1d85c2444.png

1、properties文件

配置文件里面写好你要用的配置值:

4a70f15e6a979297015a3e7a48daeaf8.png

创建获取properties对象的函数:

public static Properties loadPropertiesFromFile(String filename) throwsIOException {

Properties p= newProperties();

InputStream input= Downloader.class.getClassLoader().getResourceAsStream(filename);

p.load(input);returnp;

}

代码中调用配置量:

Properties p = loadPropertiesFromFile("downloader.properties");

String regex=p.getProperty("local_ip_regex");

2、yml文件

配置文件里面配置值的方式与properties类似,也是用 变量名=值 的方式,但是中间可以用----隔开,然后yml会自动将两个-------之间的内容解析为一个map

一个yml文件解析出来就是一个List

3、直接读取文件转化为String或InputStream

参考:、

java中读取resources目录下的配置文件:https://blog.csdn.net/xu511739113/article/details/52440982

使用inputstream按行读取文件:https://blog.csdn.net/u010889616/article/details/51477037

public staticInputStream get_whitelist_inputstream(){//获取配置文件的inputstream

ClassLoader classLoader=Downloader.class.getClassLoader();

InputStream whitelist_inputstream=classLoader.getResourceAsStream(p.getProperty("white_list_file"));returnwhitelist_inputstream;//获取配置文件的路径名//ClassLoader classLoader=Downloader.class.getClassLoader();//URL resource=classLoader.getResource(p.getProperty("white_list_file"));//String path=resource.getPath();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值