• 路径1


    java中读取src文件下属性文件(支持跨服务器)

    目前的代码如下:

    1. import java.io.File;  
    2. import java.io.FileInputStream;  
    3. import java.io.FileNotFoundException;  
    4. import java.io.IOException;  
    5. import java.io.InputStream;  
    6. import java.io.UnsupportedEncodingException;  
    7. import java.net.URLDecoder;  
    8. import java.util.Iterator;  
    9. import java.util.Properties;  
    10. import java.util.Set;  
    11. import java.util.logging.Level;  
    12. import java.util.logging.Logger;  
    13.   
    14. /** 
    15.  * 
    16.  * @author zcb 
    17.  */  
    18. public class Test {  
    19.   
    20.     public static void main(String args[]) {  
    21.   
    22.         Test test = new Test();  
    23.         InputStream in = null;  
    24.         Properties props = new Properties();  
    25.         //第一种方法,取得src下的属性文件,成功   
    26.         in = test.getClass().getResourceAsStream("/mypropertiestest.properties");  
    27.   
    28.         //第二种方法,取得src下的属性文件,相对第一种少了个“/”,注意:error,不行,此时取得的路径是到classes文件夹   
    29. //        System.out.println("path:"+test.getClass().getResource("mypropertiestest.properties").getPath());   
    30. //        in = test.getClass().getResourceAsStream("mypropertiestest.properties");   
    31.           
    32.   
    33.         //第三种种方法,通过绝对路径,取得src下的属性文件,成功,但对apusic服务器不大理想,属性文件要拷贝到项目外面   
    34. //        String filepath = test.getClass().getResource("/").getPath()  + java.io.File.separator + "mypropertiestest.properties";   
    35. //        try {   
    36. ////        filepath = filepath.substring(1).replaceAll("%20", " ");//or filepath = filepath.replaceAll("%20", " ");   
    37. //            filepath = URLDecoder.decode(filepath, "UTF-8");   
    38. //        } catch (UnsupportedEncodingException ex) {   
    39. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);   
    40. //        }   
    41. //        try {   
    42. //            in = new FileInputStream(new File(filepath));   
    43. //        } catch (FileNotFoundException ex) {   
    44. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);   
    45. //        }   
    46.         try {  
    47.             props.load(in);  
    48.         } catch (IOException e) {  
    49.             e.printStackTrace();  
    50.         } finally {  
    51.             if (in != null) {  
    52.                 try {  
    53.                     in.close();  
    54.                 } catch (IOException ex) {  
    55.                     Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);  
    56.                 }  
    57.             }  
    58.         }  
    59.   
    60.         //输出属性文件中的信息   
    61.         Set set = props.keySet();  
    62.         Iterator it = set.iterator();  
    63.         System.out.println("Begin ...");  
    64.         while (it.hasNext()) {  
    65.             String key = (String) it.next();  
    66.             System.out.println(key + "=" + props.getProperty(key));  
    67.         }  
    68.         System.out.println("End");  
    69.     }  
    70. }  
    Java代码 
    1. import java.io.File;  
    2. import java.io.FileInputStream;  
    3. import java.io.FileNotFoundException;  
    4. import java.io.IOException;  
    5. import java.io.InputStream;  
    6. import java.io.UnsupportedEncodingException;  
    7. import java.net.URLDecoder;  
    8. import java.util.Iterator;  
    9. import java.util.Properties;  
    10. import java.util.Set;  
    11. import java.util.logging.Level;  
    12. import java.util.logging.Logger;  
    13.   
    14. /** 
    15.  * 
    16.  * @author zcb 
    17.  */  
    18. public class Test {  
    19.   
    20.     public static void main(String args[]) {  
    21.   
    22.         Test test = new Test();  
    23.         InputStream in = null;  
    24.         Properties props = new Properties();  
    25.         //第一种方法,取得src下的属性文件,成功  
    26.         in = test.getClass().getResourceAsStream("/mypropertiestest.properties");  
    27.   
    28.         //第二种方法,取得src下的属性文件,相对第一种少了个“/”,注意:error,不行,此时取得的路径是到classes文件夹  
    29. //        System.out.println("path:"+test.getClass().getResource("mypropertiestest.properties").getPath());  
    30. //        in = test.getClass().getResourceAsStream("mypropertiestest.properties");  
    31.           
    32.   
    33.         //第三种种方法,通过绝对路径,取得src下的属性文件,成功,但对apusic服务器不大理想,属性文件要拷贝到项目外面  
    34. //        String filepath = test.getClass().getResource("/").getPath()  + java.io.File.separator + "mypropertiestest.properties";  
    35. //        try {  
    36. ////        filepath = filepath.substring(1).replaceAll("%20", " ");//or filepath = filepath.replaceAll("%20", " ");  
    37. //            filepath = URLDecoder.decode(filepath, "UTF-8");  
    38. //        } catch (UnsupportedEncodingException ex) {  
    39. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);  
    40. //        }  
    41. //        try {  
    42. //            in = new FileInputStream(new File(filepath));  
    43. //        } catch (FileNotFoundException ex) {  
    44. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);  
    45. //        }  
    46.         try {  
    47.             props.load(in);  
    48.         } catch (IOException e) {  
    49.             e.printStackTrace();  
    50.         } finally {  
    51.             if (in != null) {  
    52.                 try {  
    53.                     in.close();  
    54.                 } catch (IOException ex) {  
    55.                     Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);  
    56.                 }  
    57.             }  
    58.         }  
    59.   
    60.         //输出属性文件中的信息  
    61.         Set set = props.keySet();  
    62.         Iterator it = set.iterator();  
    63.         System.out.println("Begin ...");  
    64.         while (it.hasNext()) {  
    65.             String key = (String) it.next();  
    66.             System.out.println(key + "=" + props.getProperty(key));  
    67.         }  
    68.         System.out.println("End");  
    69.     }  
    70. }  

    在windows下测试通过,Linux没测试,需要进一步研究。

    补充:使用ClassLoader.getSystemResourceAsStream("/mypropertiestest.properties")和Thread.currentThread().getContextClassLoader().getResourceAsStream("/mypropertiestest.properties")读取src下的属性文件,通过测试,在windows和Linux下的tomcat和apusic都能成功。

  • 相关阅读:
    7种jvm垃圾回收器,这次全部搞懂
    3分钟学会redis主从复制搭建,及原理
    一文搞懂什么是递归,程序员必会算法之一
    Redis持久化方式:RDB和AOF详解,对比
    jvm垃圾回收算法详解,看这一篇就够了(求点赞)
    Redis命令大全,满足你的日常工作,看这一篇就够了(求点赞)
    Java自定义异常为什么性能差(求点赞)阿里双十一的性能凶手之一
    jvm类加载器,类加载机制详解,看这一篇就够了(求点赞)
    show processlist 命令详解,MySQL优化看这一篇就够了
    asp.net中的post和get请求操作
  • 原文地址:https://www.cnblogs.com/shenzhichipingguo/p/9318989.html
Copyright © 2020-2023  润新知