• java web工程读取及修改配置文件


    这篇博客比自己讲解的详细:

    http://blog.sina.com.cn/s/blog_69398ed9010191jg.html

    使用方法:

    1)配置文件在web-info的class目录下,或者说,eclipse工程的src目录下

    2)问题:修改配置文件之后,若不重启服务器,配置文件能够即刻实时生效么?自己利用tomcat测试的结果是可以,但最好进一步确认一下

    package com.bobo.util;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.util.HashMap;
    import java.util.Properties;
    
    public class PropertiesUtil {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String fileName = "test.properties";
            PropertiesUtil pu = new PropertiesUtil();
            HashMap map=new HashMap();
            map.put("age", "24");
            map.put("name", "leishao");
            pu.updateProperty(fileName, map);
            System.out.println(pu.loadAllProperties(fileName));
            System.out.println(pu.loadProperty(fileName, "name"));
            
    
        }
    
        public HashMap<String, String> loadAllProperties(String fileName) {
            InputStream is = PropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            Properties pr = new Properties();
    
            try {
                pr.load(br);
                HashMap<String, String> hm = new HashMap<String, String>();
                for (Object s : pr.keySet()) {
                    hm.put(s + "", pr.getProperty(s + ""));
                }
                return hm;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
    
                    }
                }
    
            }
            return null;
    
        }
    
        public String loadProperty(String fileName, String key) {
            InputStream is = PropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            Properties pr = new Properties();
    
            try {
                pr.load(br);
                
                if (pr.containsKey(key)) {
                    return pr.get(key) + "";
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
    
                    }
                }
    
            }
            return null;
        }
    
        public void updateProperty(String fileName, HashMap<String,String> map) {
            InputStream is = PropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            Properties pr = new Properties();
            String filePath=PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
             
                
            try {
                pr.load(br);
                pr.putAll(map);
                OutputStream out=new FileOutputStream(filePath);
                pr.save(out, "");
                
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
    
                    }
                }
    
            }
             
        }
    
        
    }
  • 相关阅读:
    KMP
    Trie 树
    Miller-Rabin质数测试
    快速幂
    Matlab 对图片的二值化处理
    huffman tree
    hdu5512-Pagodas
    迷宫
    poj2488-A Knight's Journey【DFS】
    linux操作
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/4739906.html
Copyright © 2020-2023  润新知