Java程序读取tomcat下的properties配置文件

代码如下:

//找到tomcat/etc/wx文件夹

private static String getPropFolderPath()

{

/* Properties p = System.getProperties();

p.list(System.out);*/

//获取tomcat的路径

String path = System.getProperty("catalina.home");

if (StringUtils.isEmpty(path)) {

path = QueryQRCodeUrlController.class.getResource("/").getPath();

path = new File(path).getParent() + File.separator;

} else {

path = path + File.separator + TOMCAT_PATH;

}

return path;

}

//***********************初始化配置信息************************

static{

Properties p=new Properties();

InputStream in=null;

try {

in=new FileInputStream(getPropFolderPath()+WX_PROPERTIES);

p.load(in);

VALUE = p.getProperty("wx.serviceId");

PWD = p.getProperty("wx.pwd");

} catch (Exception e) {

logger.error(e.toString());

}finally{

if(in!=null){

try {

in.close();

} catch (IOException e) {

logger.error(e.toString());

}

}

}

}

1:  File.separator   与系统有关的默认名称分隔符。

在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File ("C:\tmp\test.txt");
在Linux下则是这样的:
File file2 = new File ("/tmp/test.txt");

如果要考虑跨平台,则最好是这么写:
File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");

File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。

2: System.getProperties():该方法是获取当前系统的很多属性名和value值。得到这些键值对之后,可以通过下面方法获取value值:

System.getProperty("catalina.home");《如果清楚系统的这些属性之后,可以直接通过该方法获取相应的value值》

3: 静态代码块:

static{

System.out.println();

}

用于初始化一些信息,服务启动之后,类被加载时执行。只执行一次。

时间: 2024-08-11 09:47:40

Java程序读取tomcat下的properties配置文件的相关文章

使用spring等框架的web程序在Tomcat下的启动顺序及思路理清

大牛请绕过,此文仅针对自己小白水平,对web程序的启动流程做个清晰的回顾. 一.使用spring等框架的web程序在Tomcat下的启动流程 1)Tomcat是根据web.xml来启动的.首先到web.xml 2)web.xml中负责启动spring和spring mvc.对应的启动配置文件分别是 启动spring mvc,并进行所有资源路径映射 <servlet> <servlet-name>springMVC</servlet-name> <servlet-c

JAVA程序获取Tomcat的运行状态

用浏览器来查看Tomcat的运行状态: 配置Tomcat的管理用户和权限. 打开%Tomcat_Home%/conf/目录下的tomcat-user.xml文件,配置以下内容: <role rolename="manager-status"/> <role rolename="manager"/>   <role rolename="manager-jmx"/>  <role rolename="

Java程序读取Properties文件

1.读取文件代码如下: /** * 配置文件为settings.properties * YourClassName对应你类的名字 * / private Properties LoadSettings() { Properties prop = new Properties(); try { InputStream in = new BufferedInputStream(YourClassName.class.getClassLoader().getResourceAsStream("/se

java程序读取时分秒为00:00:00

今儿个碰到一问题 搞了半天 数据库中datetime 时间为2014-12-13 12:20:01 java程序中读取却未能到2014-12-13 00:00:00 纳闷了 bean 定义类型为java.util.Date mybatis中配置 用户配置文件自动生成的 <result column="last_online_time" property="lastOnlineTime" jdbcType="DATE" /> 将如上配置

java程序读取资源文件时路径如何指定

java程序,读取资源操作有两种方式. 一是直接通过File进行的.例如FileReader.BufferedReader等.文件目录就是相对于Project了.如我新建一个MyProject,那么,我在MyProject下有一个icons文件夹,如果想读取icons里面的东西,路径是诸如"icons/***.gif"的相对路径就可以了. 二是通过getClass().getResource("")得到.第二种方法必须保证文件是在classpath包括的路径下.而一

Java读取WEB-INF目录下的properties配置文件

文件位置: 代码: public static Properties getProperties(){ Properties pts = new Properties (); FileInputStream in = null; try{ String path = UtilManager.class.getResource("/").getPath(); String websiteURL = (path.replace("/build/classes", &qu

Java 程序读取properties文件

新建一个数据库properties文件 dataSource.properties 新建一个测试类 import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import

Java读取利用java.util类Properties读取resource下的properties属性文件

说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.ResourceBundle; public class Test { private static Properties pro ; static{ InputStream inputStream = Test.class.ge

Tomcat 下4个配置文件详解

Tomcat 的配置文件由4个 xml 文件构成,context.xml.web.xml.server.xml.tomcat-users.xml 这4个文件.每个文件都有自己的功能与配置方法,下列将逐一介绍这几个文件的配置: 一.context.xml 文件 Context.xml 是 Tomcat 公用的环境配置,tomcat 服务器会定时去扫描这个文件.一旦发现文件被修改(时间戳改变了),就会自动重新加载这个文件,而不需要重启服务器.推荐在 $CATALINA_BASEconf/contex