zoukankan      html  css  js  c++  java
  • java 对properties 文件的写操作

     

     调用:

     

    还有一个问题没有解决,就是如果文件在本项目中,读进来的应该是一个流。有没有处理过的。谢谢...


    对存放到**.properties 文件中的内容进行读取,当**.properties 内容有变更时,直接修改,不需要重新启动服务器

    package com.rt.portal.web.guangxi.ICBC.clrcenter.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.net.URL;
    import java.util.Properties;
    
    
    /**
     *<pre>
     * COMPANY 海科融通 
     * @author liucheng
     * MSN     soliucheng@hotmail.com
     * 2012-2-28 上午09:27:14
     * ALL_RIGHT: 2011-2015 
     * </pre>
     */
    public class FtpUtil {
    	private static FtpUtil m_instance;
    	private static String PFILE;
    
    	synchronized public static FtpUtil getInstance() {
    		if (m_instance == null) {
    			if (PFILE == null) {
    				URL url =Thread.currentThread().getContextClassLoader().getResource("ftp.properties");
    			}
    			m_instance = new FtpUtil();
    		}
    		return m_instance;
    	}
    
    	private File m_file = null;
    	private long m_lastModifiedTime = 0;
    	private Properties m_props = null;
    
    	private FtpUtil() {
    		m_file = new File(PFILE);
    		m_lastModifiedTime = m_file.lastModified();
    
    		if (m_lastModifiedTime == 0) {
    			System.err.println(PFILE + " file does not exist!");
    		}
    
    		m_props = new Properties();
    
    		try {
    			m_props.load(new FileInputStream(PFILE));
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	final public String getConfigItem(String name, String defaultVal) {
    		long newTime = m_file.lastModified();
    		if (newTime == 0) {
    			if (m_lastModifiedTime == 0) {
    				System.err.println(PFILE + " file does not exist!");
    			} else {
    				System.err.println(PFILE + " file was deleted!!");
    			}
    			return defaultVal;
    		} else if (newTime > m_lastModifiedTime) {
    			m_props.clear();
    			try {
    				m_props.load(new FileInputStream(PFILE));
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		m_lastModifiedTime = newTime;
    
    		String val = m_props.getProperty(name);
    		if (val == null) {
    			return defaultVal;
    		} else {
    			return val;
    		}
    	}
    	public static void main(String args []){
    		String userName = FtpUtil.getInstance().getConfigItem("FTPTRANSFERPART","192.168.30.36");
    		System.out.println(userName);
    	}
    }
    

    对固定不变的**.properties 对容进行读取,入到一个对象变量里

    	public static String GXIP = null;
    	public static String GXPORT = null;	
    static {
    		Properties props = new Properties();
    		InputStream in = null;
    		try {
    			in = Thread.currentThread().getContextClassLoader().getResourceAsStream("define.properties");
    			props.load(in);
    			Enumeration<?> en = props.propertyNames();
    			while (en.hasMoreElements()) {
    				String key = (String) en.nextElement();
    				if (key.equals("GXIP")) {
    					GXIP = props.getProperty(key);
    				} else if (key.equals("GXPORT")) {
    					GXPORT = props.getProperty(key);
    				}
    			}
    		}catch (Exception e) {
    		}finally {
    			if (in != null) {
    				try {
    					in.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}


    还有一种简单的方式来读取xx.properties 文件

    public class ReadProperties {
    
    	public static ResourceBundle resources = ResourceBundle.getBundle("config/"+File.separator+ "spy");
    
    	public static String getString(String str) {
    		return resources.getString(str);
    	}
    
    	public static void main(String[] args) {
    		System.out.println(getString("module.log"));
    	}
    }
    



     

  • 相关阅读:
    docker 上的第一个网址
    redis 和mongodb的区别
    在docker 里 搭建redis 主从节点
    .Net AOP 的简单入门 (静态代理 适配器模式完成aop)
    CocoaPods pod install下载慢问题
    [OC]OC基础概念
    [Swift]swift之随机数
    [Swift]iOS开发之气泡对话框的实现
    [Swift]CoreData防止数据冲突
    [Swift]iOS开发之初识CoreData
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218378.html
Copyright © 2011-2022 走看看