此文章是从网上看到一篇实用小文章,感觉不过,摘录下来的!如有问题,可及时联系,可立刻做相应处理!

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文件的解析的更多相关文章

  1. 【Java】Properties文件的解析

    public abstract class ReadProperties { public ReadProperties() {} /** * 回调函数,由调用者处理 * @param key * @ ...

  2. log4j.properties文件无法解析

    普通工程:log4j.properties文件必须放在src根目录下

  3. 自定义Yaml解析器替换Properties文件

    自定义Yaml解析器替换Properties文件 项目结构 案例代码 配置类SpringConfiguration @Configuration @Import(JdbcCofnig.class) @ ...

  4. java解析properties文件

    在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...

  5. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  6. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...

  7. Python:解析properties文件

    在项目中遇到解析properties的情况,而Python中正好没有解析properties文件的现成模块,于是从网上找到了这个脚本,有一些小地方修改了一下 原博客: Python读写properti ...

  8. maven 项目打包时无法解析读取properties文件

    在做项目时遇见一个问题,无法解析properties文件的 内容 异常为 Could not resolve placeholder ......... 在此之前均有做相关的 配置 但是从未出现过如上 ...

  9. properties 文件解析

    1.提供properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/future?useUn ...

随机推荐

  1. MFC开发小技巧总结

    1.在类向导里面可以为对话框添加方法. 2.如要添加变量,直接右击添加变量即可. 3.若对某个控件添加方法或者称之为消息处理函数,直接右击添加事件处理程序即可.  

  2. CSS环绕球体的旋转文字-3D效果

    代码地址如下:http://www.demodashi.com/demo/12482.html 项目文件结构截图 只需要一个html文件既可: 项目截图: 代码实现原理: 该示例的实现过程很简单,主要 ...

  3. 在MyEclipse上部署Tomcatserver

    我在前面的博客中介绍了在Windows下安装Tomcatserver.这篇博客将介绍在MyEclipse上部署Tomcatserver 第一步:启动MyEclipse 第二步:选项Window-> ...

  4. PHP第四课 了解经常使用的函数

    学习概要: 一.语言结构 二.自己定义函数 三.变量作用域 四.静态变量 五.函数返回值 六.參数 七.默认參数 八.引用參数 九.可变个数函数 十.回调函数 十一.变量函数 十二.递归函数 十三.文 ...

  5. GIT客户端的使用【原创】

    这次分享的方式,采用的是视频的形式,视频是本人录制. 在做项目使用SVN的时候经常有各种错误出现,所以萌发使用git的想法.在学习git的过程中发现一个神器就是分支,虽然在SVN里也有分支,但由于机制 ...

  6. php html_entity_decode使用总结

    在处理网页字符串的时候,尤其是做爬虫类的应用时,经常会涉及到要处理的字符串中包含html标签,现在对这类字符串的处理做一个小的总结: 有时候获取到的字符串中有html标签,在入库的时候出于安全的考虑通 ...

  7. hibernate QBC查询

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <>  Restrictions.ne() 不等于not equal >  Restrict ...

  8. ios 添加全屏返回手势

    1 建立导航控制器 2.导航控制器添加如下代码 - (void)viewDidLoad { [super viewDidLoad]; id target = self.interactivePopGe ...

  9. python 基础 1.2--pycharm 的安装及使用

    一. windows 先安装pycharm. PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,其提供了一个带编码补全,代码片段,支持代码折 ...

  10. EasyPlayerPro Windows播放器进行本地对讲喊话音频采集功能实现

    需求 在安防行业应用中,除了在本地看到摄像机的视频和进行音频监听外,还有一个重要的功能,那就是对讲. EasyPlayerPro-win为了减轻二次开发者的工作量,将本地音频采集也进行了集成: 功能特 ...