JAVA配置文件取值方法
来源:互联网 发布:虎门巨新网络跑路后续 编辑:程序博客网 时间:2024/06/15 09:54
其实是很简单基础的东西,但对于初学者来说多掌握点东西总是好的,多的不说,直接上代码了,最简单粗暴的方式。
一、XML配置文件:
1.GlobalConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <db_ip>192.168.10.229</db_ip> <db_name>nest_test</db_name> <user_name>root</user_name> <password>root</password> <url>http://192.168.0.1:8080/SMSServer/SMSServlet</url> <templateId>213047611111</templateId> </Settings>
package xmlhelper;public class XmlGlobal {public static String Db_IP="";public static String User_Name="";public static String Pass_Word="";public static boolean isConsolePro = false;public static String Db_Name="";public static String url="";public static String templateId="";}
package xmlhelper;import java.io.FileInputStream;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;public class GlobalXml {public static void xmlParser(){try {String path="./resource/SysConfig/GlobalConfig.xml";FileInputStream fi=new FileInputStream(path);SAXBuilder builder = new SAXBuilder(false);Document read_doc = builder.build(fi);Element root = read_doc.getRootElement();XmlGlobal.Db_IP=root.getChild("db_ip").getValue();XmlGlobal.Db_Name=(root.getChild("db_name").getValue());XmlGlobal.User_Name=root.getChild("user_name").getValue();XmlGlobal.Pass_Word=root.getChild("password").getValue();XmlGlobal.url=root.getChild("url").getValue();XmlGlobal.templateId=root.getChild("templateId").getValue();} catch (Exception e) {e.printStackTrace();}}}调用:
String id = XmlGlobal.templateId;
二、properties文件:
1.configure.properties配置文件
SMS_url=https://www.baidu.com/20141029AUTH_TOKEN=39cde072ec96APP_ID=32fd03c4db6444SERVER_IP=192.168.10.123:8080
2.ConfigureResource.java读取配置文件
package com.fisee.utils;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;public class ConfigureResource {private static ResourceBundle r; private static InputStream in;static{ try { String path = System.getProperty("user.dir").replaceAll("\\\\", "/"); //System.out.println(path.replaceAll("bin", ""));in = new BufferedInputStream(new FileInputStream(path.replaceAll("bin", "")+"webapps/SMSServer/resource/configure.properties"));r = new PropertyResourceBundle(in); } catch (Exception e) {e.printStackTrace();}} public static String getParam(String key) { try { String s = new String(r.getString(key).getBytes("ISO-8859-1"),"GBK"); if(s.equalsIgnoreCase("SPACE")) s = " ";return s;} catch (Exception e) {System.out.println(e);return "Unknown";} } public static void main(String[] args) {System.out.println(ConfigureResource.getParam("SMS_url"));//取值}}
0 0
- JAVA配置文件取值方法
- JAVA 連接數據庫配置文件讀取方法
- 配置文件与工程同一级目录取值方法
- java常用map取值方法
- java取整方法
- Java读配置文件方法
- java读取配置文件方法
- java取四舍五入的方法
- java取四舍五入的方法
- 取Java绝对路径的方法
- Java取整方法总结
- java 取交集方法retainAll
- shell取 配置文件内 参数值
- java读取配置文件方法大全
- java读取配置文件的方法
- JAVA-Map集合中的keySethe和entry取值方法
- java Map key value 取值的方法
- stuts2 取值方法
- Spring Maven POM.xml
- fix bug滑动视图 动画卡ui
- 浮点数不用科学计数法
- sublime Emmet的用法及相关语法
- SQLServer之创建表格,向表格中插入数据
- JAVA配置文件取值方法
- 剑指offer第十四题【链表中倒数第k个结点】c++实现
- Unified Occlusion Culling: Portals, Visibility Umbra, and HZB
- android:layout_weight的真实含义
- Android属性动画之XML定义方式
- mysql set赋值与select into 赋值的区别
- iOS面试攻略下篇:Objective-C面试题和基本概念(1)
- <LeetCode><Easy> 190 Revese Bits
- 自旋锁(spinlock) 解释得经典,透彻