properties文件的解析
此文章是从网上看到一篇实用小文章,感觉不过,摘录下来的!如有问题,可及时联系,可立刻做相应处理!
Java读取.properties
配置文件的几种方法
在做java工程时,
经常会将一些配置信息放到
.properties
文件中,
需要用时再去对应的文件中
读取,下面提供几种常用的读取
.properties
文件的方法,并列举这些方法的特点。
一、读文件方式读取
.properties
文件。
关键代码如下:
try{
// 创建Properties对象
Properties p = new Properties();
// 设置读取文件路径
String s_xmlpath="config.properties";
InputStream io=Test.class.getClassLoader().getResourceAsStream(s_xmlpath);
// 加载文件
p.oad(io);
// 取得文件的值
system.out.println(p.getProperty("key"));
// 关闭流
io.close();
}catch(Exception ex){
ex.printStackTrace();
}
该方法可放到
Servlet
中,在工程初期化时一次性加载配置文件,具体代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
public static Properties p;
private static Test instance = null;
/**
* Constructor of the object.
* 默认构造方法
*/
public Test() {
super();
}
/**
* Constructor of the object.
* 私有构造方法,调用
init()
方法读取配置文件
*/
private Test(String str) {
super();
try {
this.init();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 静态方法,取得对象实例
* @return
*/
public static Test getInstance()
{
// 当前实例为空时
if(instance == null)
{
instance = new Test("");
}
return instance;
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
* This method is called when a form has its tag value method equals to get.
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
……
}
/**
* The doPost method of the servlet. <br>
* This method is called when a form has its tag value method equals to post.
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
……
}
/**
* Initialization of the servlet. <br>
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
try{
// 创建Properties对象
p = new Properties();
// 设置读取文件路径
String s_xmlpath="sql.properties";
InputStream io=Test.class.getClassLoader().getResourceAsStream(s_xmlpath);
//调用里面的信息
}
properties文件的解析的更多相关文章
- 【Java】Properties文件的解析
public abstract class ReadProperties { public ReadProperties() {} /** * 回调函数,由调用者处理 * @param key * @ ...
- log4j.properties文件无法解析
普通工程:log4j.properties文件必须放在src根目录下
- 自定义Yaml解析器替换Properties文件
自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...
- java解析properties文件
在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- Spring系列之——springboot解析resources.application.properties文件
摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...
- Python:解析properties文件
在项目中遇到解析properties的情况,而Python中正好没有解析properties文件的现成模块,于是从网上找到了这个脚本,有一些小地方修改了一下 原博客: Python读写properti ...
- maven 项目打包时无法解析读取properties文件
在做项目时遇见一个问题,无法解析properties文件的 内容 异常为 Could not resolve placeholder ......... 在此之前均有做相关的 配置 但是从未出现过如上 ...
- properties 文件解析
1.提供properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/future?useUn ...
随机推荐
- 各种常用的CDN加速服务
各种CDN加速网址:点击此处 1.Echar <script type="text/javascript" src="http://echarts.baidu.co ...
- fabric使用实例(发布web包的一个例子)
#!/usr/bin/env python # -*- coding: utf-8 -* #添加中文注释的编码 #fabfile.py from fabric.api import * env.use ...
- XMPP系列(三)---获取好友列表、加入好友
1.心跳检測.掉线重连功能 client和server端都能够设置多久发送一次心跳包,假设对方没有返回正确的pong信息,则会断开连接,而加入掉线重连功能,则会自己主动进行连接. 假设自己写聊天功能还 ...
- vim 穿越时空
1. 回到以前的文件状态 :earlier 3m 回到文件3分钟之前的状态 2. 回到以后的文件状态 :later 3m 回到文件3分钟之后的状态 3. 时间单位 s 秒 m 分钟 d ...
- hadoop生态系统学习之路(六)hive的简单使用
一.hive的基本概念与原理 Hive是基于Hadoop之上的数据仓库,能够存储.查询和分析存储在 Hadoop 中的大规模数据. Hive 定义了简单的类 SQL 查询语言,称为 HQL.它同意熟悉 ...
- JavaWeb学习笔记:Servlet
Servlet JavaWeb 概念 Java Web应用由一组Servlet.HTML页面.类.以及其他能够被绑定的资源构成. 他能够在各种供应商提供的实现Servlet规范的Servlet容器中执 ...
- 微服务之旅:从Netflix OSS到 Istio Service Mesh
在这篇文章中,我们从Netflix开始,通过Envoy和Istio的崛起,快速浏览微服务的历史. 微服务是具有边界上下文的松散耦合服务,使您能够独立开发,部署和扩展服务.它还可以定义为构建独立开发和部 ...
- matlab中文显示乱码:控制台上的,编辑器的,图片中的
问题:matlab脚本与函数文件的中文注释显示乱码. 环境:matlab R2016a.Windows 10 home. 解决方案: step1 检查locale值 matlab命令行键入命令 fea ...
- 精彩回顾 HUAWEI HiAI 亮相华为北研所
从普通照片变成艺术品,仅需3秒: 从随手拍下的讲解胶片到生成规整清晰的ppt,只要瞬间…… 5月25日在华为北京研究所举办的HUAWEI HiAI技术合作交流会上,伴随着一声声惊叹,数款接入HUA ...
- ejabberd日志分析客户端登录流程
通过ejabberd的日志,整理了下客户端登录流程. 1. 通过TCP连接5222端口的流程: (1) 客户端向服务器发送stream流 <stream:stream to="nba. ...