server-config-w.properties 该文件放在src目录下面
- upload=D:/upload/
- conn_driver=test_1
- ftp_choice_auto=test_2
- ftp_ip=test_3
- ftp_passWord=test_4
- conn_userName=test_5
- ftp_choice_store=test_6
- file_store_path=test_7
- conn_type=test_8
- conn_passWord=test_9
- ftp_userName=test_10
- conn_url=test_11
- ftp_port=test_12
upload=D:/upload/ conn_driver=test_1 ftp_choice_auto=test_2 ftp_ip=test_3 ftp_passWord=test_4 conn_userName=test_5 ftp_choice_store=test_6 file_store_path=test_7 conn_type=test_8 conn_passWord=test_9 ftp_userName=test_10 conn_url=test_11 ftp_port=test_12
获取应用程序路径类:
- package com.hzzy.pqgl.util;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLDecoder;
- import org.apache.log4j.Logger;
- public class FileTool {
- private static Logger logger = Logger.getLogger("com.auib.util.FileTool");
- public static String getAppPath() {
- // 获得应用程序路径
- URL appUrl = FileTool.class.getClassLoader().getResource("");
- String appPath = appUrl.getPath();
- if (WINDOWS && appPath.startsWith("/"))
- appPath = appPath.substring(1);
- try {
- appPath = URLDecoder.decode(appPath, "UTF-8");
- // appPath = System.getProperty("user.dir");
- } catch (UnsupportedEncodingException e) {
- }
- return appPath;
- }
- private static final boolean WINDOWS = System.getProperty("os.name")
- .startsWith("Windows");
- }
package com.hzzy.pqgl.util; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import org.apache.log4j.Logger; public class FileTool { private static Logger logger = Logger.getLogger("com.auib.util.FileTool"); public static String getAppPath() { // 获得应用程序路径 URL appUrl = FileTool.class.getClassLoader().getResource(""); String appPath = appUrl.getPath(); if (WINDOWS && appPath.startsWith("/")) appPath = appPath.substring(1); try { appPath = URLDecoder.decode(appPath, "UTF-8"); // appPath = System.getProperty("user.dir"); } catch (UnsupportedEncodingException e) { } return appPath; } private static final boolean WINDOWS = System.getProperty("os.name") .startsWith("Windows"); }
读取配置文件单例类:
- package com.hzzy.pqgl.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.Properties;
- /**
- * 读取属性文件的配置参数
- */
- public class ServerConfig {
- //private static Logger logger = Logger.getLogger("product run.trns.core.parse.NodeParser");
- // 属性配置
- private static Properties log = null;
- // 单例模式
- private static ServerConfig config = null;
- // 判断当前系统环境
- private static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
- // 私有构造
- private ServerConfig(){ }
- // 单例模式初始化
- public static ServerConfig getInstance(){
- if(config == null){
- config = new ServerConfig();
- init();
- }
- return config;
- }
- // 初始化函数
- private static void init(){
- if (log == null){
- String configfile = null;
- if(WINDOWS)
- configfile = "server-config-w.properties";
- else
- configfile = "server-config-l.properties";
- String appPath = FileTool.getAppPath();
- File file = new File(appPath);
- String appPathParent = file.getParent();
- String configPath = appPathParent;
- appPath= appPath + File.separator + configfile;
- file = new File(appPath);
- FileInputStream fis = null;
- try{
- if (!file.exists()){
- System.out.println("配置文件不存在");
- System.exit(-1);
- }
- fis = new FileInputStream(file);
- log = new Properties();
- log.load(fis);
- //conn_driver=test_1
- log.setProperty("ConfigPath", configPath);
- fis.close();
- }
- catch(Exception e){
- //logger.error("config::init::cann't load log file" + e.getMessage());
- System.exit(-1);
- }
- }
- }
- public Properties getWorkInfo(){
- return log;
- }
- public String getProperty(String key){
- return log.getProperty(key);
- }
- /*
- public synchronized void writeLog(String key, String value) throws Exception{
- setProperty(key, value);
- try{
- save();
- }
- catch(Exception e){
- throw new Exception(e.getMessage());
- }
- }
- */
- /*
- private void setProperty(String key, String value){
- log.setProperty(key, value);
- }
- private void save() throws Exception{
- FileOutputStream fos = null;
- try{
- fos = new FileOutputStream(file);
- log.store(fos, "");
- fos.close();
- }
- catch(Exception e){
- throw new Exception(e.getMessage());
- }
- }
- */
- }
package com.hzzy.pqgl.util; import java.io.File; import java.io.FileInputStream; import java.util.Properties; /** * 读取属性文件的配置参数 */ public class ServerConfig { //private static Logger logger = Logger.getLogger("product run.trns.core.parse.NodeParser"); // 属性配置 private static Properties log = null; // 单例模式 private static ServerConfig config = null; // 判断当前系统环境 private static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows"); // 私有构造 private ServerConfig(){ } // 单例模式初始化 public static ServerConfig getInstance(){ if(config == null){ config = new ServerConfig(); init(); } return config; } // 初始化函数 private static void init(){ if (log == null){ String configfile = null; if(WINDOWS) configfile = "server-config-w.properties"; else configfile = "server-config-l.properties"; String appPath = FileTool.getAppPath(); File file = new File(appPath); String appPathParent = file.getParent(); String configPath = appPathParent; appPath= appPath + File.separator + configfile; file = new File(appPath); FileInputStream fis = null; try{ if (!file.exists()){ System.out.println("配置文件不存在"); System.exit(-1); } fis = new FileInputStream(file); log = new Properties(); log.load(fis); //conn_driver=test_1 log.setProperty("ConfigPath", configPath); fis.close(); } catch(Exception e){ //logger.error("config::init::cann't load log file" + e.getMessage()); System.exit(-1); } } } public Properties getWorkInfo(){ return log; } public String getProperty(String key){ return log.getProperty(key); } /* public synchronized void writeLog(String key, String value) throws Exception{ setProperty(key, value); try{ save(); } catch(Exception e){ throw new Exception(e.getMessage()); } } */ /* private void setProperty(String key, String value){ log.setProperty(key, value); } private void save() throws Exception{ FileOutputStream fos = null; try{ fos = new FileOutputStream(file); log.store(fos, ""); fos.close(); } catch(Exception e){ throw new Exception(e.getMessage()); } } */ }
测试类:
- package com.hzzy.pqgl.util;
- public class Test {
- public static void main(String args[]) {
- String value = ServerConfig.getInstance().getWorkInfo()
- .getProperty("upload");
- }
- }