Java Web读取properties配置文件
来源:互联网 发布:sql增删改查 编辑:程序博客网 时间:2024/05/22 09:38
java action读取src目录下的properties配置文件。
mailServer.properties配置文件如下:
mailServerHost = smtp.163.commailServerPort = 25authValidate = trueuserName = test@163.com
读取配置文件类GetProperty代码如下:
package com.hsinghsu.test.action;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class GetProperty {// 方法一:通过java.util.ResourceBundle读取资源属性文件public static String getPropertyByName(String path, String name) {String result = "";try {// 方法一:通过java.util.ResourceBundle读取资源属性文件result = java.util.ResourceBundle.getBundle(path).getString(name);System.out.println("name:" + result);} catch (Exception e) {System.out.println("getPropertyByName2 error:" + name);}return result;}// 方法二:通过类加载目录getClassLoader()加载属性文件public static String getPropertyByName2(String path, String name) {String result = "";// 方法二:通过类加载目录getClassLoader()加载属性文件InputStream in = GetProperty.class.getClassLoader().getResourceAsStream(path);// InputStream in =// this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");// 注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用// InputStream in =// Object.class.getResourceAsStream("/mailServer.properties");Properties prop = new Properties();try {prop.load(in);result = prop.getProperty(name).trim();System.out.println("name:" + result);} catch (IOException e) {System.out.println("读取配置文件出错");e.printStackTrace();}return result;}}
action代码如下:
调用action,即可获取相应配置文件的属性值。package com.hsinghsu.test.action;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {private static final long serialVersionUID = 3348881101306356364L;public String test(){System.out.println("=="+GetProperty.getPropertyByName("mailServer","userName"));System.out.println("==>>"+GetProperty.getPropertyByName2("mailServer.properties","userName"));////以下参数从properties文件读取//String mailServerHost = null; // 发送邮件的服务器的IP//String mailServerPort = null; // 发送邮件的服务器端口//String userName = null; // 登陆邮件发送服务器的用户名//boolean authValidate = false; // 是否需要身份验证////try {////方法一:通过java.util.ResourceBundle读取资源属性文件//mailServerHost = java.util.ResourceBundle.getBundle("mailServer").getString("mailServerHost");//System.out.println("mailServerHost:"+mailServerHost);//} catch (Exception e) {//System.out.println("mailServerHost error:"+mailServerHost);//}//////方法二:通过类加载目录getClassLoader()加载属性文件////InputStream in = TestAction.class.getClassLoader().getResourceAsStream("mailServer.properties");//InputStream in = this.getClass().getClassLoader().getResourceAsStream("mailServer.properties");//////注:Object.class.getResourceAsStream在action中调用报错,在普通java工程中可用////InputStream in = Object.class.getResourceAsStream("/mailServer.properties"); //Properties prop = new Properties();//try {//prop.load(in);//mailServerHost = prop.getProperty("mailServerHost").trim();//mailServerPort = prop.getProperty("mailServerPort").trim();//userName = prop.getProperty("userName").trim();//authValidate = prop.getProperty("authValidate").trim().equalsIgnoreCase("true");////System.out.println("mailServerHost:"+mailServerHost+" mailServerPort:"+mailServerPort+" userName:"+userName+" authValidate:"+authValidate);//} catch (IOException e) {//System.out.println("读取邮箱服务配置文件出错");//e.printStackTrace();//} // return null;}}
- Java Web读取properties配置文件
- Java Web读取properties配置文件
- java web项目读取配置文件(.properties)
- Java Web项目如何读取.properties配置文件
- java 读取properties配置文件
- Java读取properties配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java 读取*.properties配置文件
- Java读取properties配置文件
- java 读取配置文件(properties)
- java读取 .properties配置文件
- java读取properties配置文件
- Java读取properties配置文件
- Java读取properties配置文件
- java读取配置文件properties
- java读取properties配置文件
- java读取properties配置文件
- latex plugin for inkscape
- [转载备查]jQuery1.9中被删除的API
- 黑马程序员_Java基础Day03(下)_函数(Done)
- linux线程通信:sigwait和pthread_kill
- uva 190 Circle Through Three Points (计算几何) 面向对象的封装
- Java Web读取properties配置文件
- 基于web的远程led控制
- 一条命令彻底解决Xterm的中文显示问题
- 程序人生
- springMVC框架搭建
- Windows主机端与自定义USB HID设备通信详解
- Linux poll机制
- 对象和类
- 移动互联网银行