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

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. 浅谈js中的MVC

    MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器) 本文将用一个经典的例子todoList来展开 一个事件发生的过程(通信单向流动): 1.用户在视图V ...

  2. RGB颜色空间alpha混合的方法

    http://blog.csdn.net/xhhjin/article/details/6444782http://blog.csdn.net/xhhjin/article/details/64454 ...

  3. Oracle:创建存储过程

    1.无参存储过程 create or replace procedure test_procasv_total number(10);begin  select count(*) into v_tot ...

  4. jquery单选框radio绑定click事件实现方法

    本文实例讲述了jquery单选框radio绑定click事件实现方法.分享给大家供大家参考. 具体实现方法如下: 复制代码代码如下: <html><head><title ...

  5. markdownPad2 绿色破解版安装

    markdownpad2,默认为markdown传统风格,不能编辑分行的代码段,可以在设置里改为Github 风格,但需要付费激活,以下是绿色安装包,附激活方法 下载地址 https://pan.ba ...

  6. PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案

    一:PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案 (1) php服务器把图片处理成缩率图或指定大小的图片在通过PHP程序代码 操作FTP 上传到图片服务器. 二:图片服务器 ...

  7. POJ 3895 Cycles of Lanes (dfs)

    Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...

  8. 《Java设计模式》之构建者模式

    概述:          构造者模式(Builder Pattern):构造者模式将一个复杂对象的构造过程和它的表现层分离开来.使得相同的构建过程能够创建不同的表示,又称为生成器模式.      Bu ...

  9. go test 下篇

    前言 go test 上篇 给大家介绍了golang自带的测试框架,包括单元测试和性能测试.但是在实际生产中测试经常会遇到一些网络或者依赖的第三方系统接口,运行测试用例的时候希望忽略这些接口的实际依赖 ...

  10. GS(道具,帮会)定时存储

    //最近数据库存储做了重大改变,数据库内部的回头再说,先看看GS这边的 .现在感觉数据库的状态将请求包放入命令队列中,以前是全部放进去,这样让其他的数据库操作不会随着数据库定时器而变慢,GS线程去驱动 ...