读取配置文件,先读取config目录,再读取user.dir目录,再读取默认classpath目录。
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class PropertiesUtil {public static Properties getProperties(String fileName) {try {String outPath = System.getProperty("user.dir") + File.separator + "config" + File.separator + fileName;//先读取config目录的,没有再加载classpath的if (!new File(outPath).exists()) {outPath = System.getProperty("user.dir") + File.separator + fileName;}Properties properties = new Properties();InputStream in = new FileInputStream(new File(outPath));properties.load(in);return properties;} catch (IOException e) {System.out.println(e.getMessage());try {Properties properties = new Properties();InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);//默认加载classpath的properties.load(in);return properties;} catch (IOException es) {System.out.println(es.getMessage());return null;}}}}
