
1 package com.example.tempshiro.util; 2 3 import java.io.InputStream; 4 import java.util.Properties; 5 6 public class LoadPropertiesUtil { 7 8 public static Properties loadProperties(String name) { 9 Properties prop = null; 10 try { 11 prop = new Properties(); 12 //不同加载方式要求的文件路径不同 13 //1. 14 // InputStream is = new FileInputStream("src/main/resources/test.properties"); 15 //2. 16 // InputStream is = Thread.currentThread().getContextClassLoader() 17 // .getResourceAsStream("test.properties"); 18 //3. 19 // InputStream is = Properties.class 20 // .getResourceAsStream("/test.properties"); 21 //4. 22 InputStream is = ClassLoader 23 .getSystemResourceAsStream("test.properties"); 24 prop.load(is); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 return prop; 29 } 30 31 public static void main(String[] args) { 32 Properties properties = loadProperties("test.properties"); 33 System.out.println(properties.get("name")); 34 } 35 }
原文:https://www.cnblogs.com/mistyTime-2016/p/14698347.html