当前位置:首页 » 编程语言 » java读取配置文件properties

java读取配置文件properties

发布时间: 2023-09-10 02:18:36

java中怎么读取properties文件

(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

② JAVA中如何读取src下所有的properties文件

1.使用java.util.Properties类的load()方法


示例:

//文件在项目下。不是在包下!!

InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;

Properties p = new Properties();

p.load(in) ;

String className2 = p.getProperty("database.driver");

String url = p.getProperty("database.url");

String user = p.getProperty("database.user");

String password = p.getProperty("database.pass");

总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println(path);

InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));

Properties prop = new Properties();

热点内容
php获取浏览器 发布:2025-03-11 09:03:31 浏览:876
安卓常驻后台需要什么权限 发布:2025-03-11 08:58:26 浏览:180
绿源电动车威牛是什么配置 发布:2025-03-11 08:47:34 浏览:9
wps加密文件密码忘记 发布:2025-03-11 08:36:49 浏览:46
可编程渲染管线 发布:2025-03-11 08:35:23 浏览:454
一般人手机设置密码会是什么 发布:2025-03-11 08:27:19 浏览:415
缓存电视剧软件 发布:2025-03-11 08:26:26 浏览:134
安卓怎么下载ios14 发布:2025-03-11 08:25:50 浏览:566
软件调试源码 发布:2025-03-11 08:24:59 浏览:488
剪辑视频怎么配置解说 发布:2025-03-11 08:24:23 浏览:264