java使用properties文件
2024-10-15 19:17:23
使用对用的util包下的properties包就可以了,这样我们有配置的话,写到一个properties文件中更直观。
这里写一个比较丑的例子:
package com.property; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties; public class MainProperty {
public static void main(String[] args) throws IOException {
Properties properties=new Properties();
//路径
String url=System.getProperty("user.dir");
System.out.println(url);
File dire=new File("");
String URI=dire.getCanonicalPath();
System.out.println(URI);
//包名
String packString=(new MainProperty()).getClass().getPackage().getName();
System.out.println(packString);
FileInputStream fis=new FileInputStream(url+"\\src\\main\\java\\com\\property\\my.properties");
properties.load(fis);
fis.close();
System.out.println("sitename:"+properties.getProperty("sitename"));
System.out.println("siteURL:"+properties.getProperty("siteURL"));
}
}
确实比较丑,里面没有自动获取到当前的配置路径。这里可以自己写一个,比如类似spring的获取当前路径下的配置文件ClassPathXmlApplicationContext的,等用的时候再写吧。还缺少了一个properties文件,里面写几个自己想要的键值对就好了,然后打印自己的内容。
最新文章
- cmake cannot find package
- python 面向对象(类)
- iOS 调用拍照、选择本地相册、上传功能---未完善。
- Eclipse程序员要掌握的常用快捷键
- vbs xml 解析
- Custom Date tag
- SVN中取消冲突conflict
- linux下Memcached安装以及PHP的调用
- 使MySQL 支持繁体字
- for循环例题
- 基于SAE+CodeIgniter3.0+管理端angularjs+前台amazeui的多用户博客系统V1.0--系统设计(一)
- 报错:loaded the ";"; nib but didn't get a UITableView
- HDU 5455 Fang Fang 水题,但题意描述有问题
- 宿主机共享文件夹给不同Linux虚拟机的方法
- MySQL数据库索引简介
- 结合JDK源码看设计模式——迭代器模式
- [BZOJ 4152][AMPPZ 2014]The Captain
- .net core使用ViewComponent将页面图片转码成base64
- js表单提交到后台对象接收
- vue缓存页面【二】