java获取properties配置文件事例
java获取properties配置文件例子
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] arg){ Properties pro=getProperties(); String yourParameter = pro.getProperty("yourParameter"); System.out.println(yourParameter); } //获取属性文件 private static Properties getProperties(){ String tomcatPath = Test.class.getClassLoader().getResource("").toString(); System.out.println(tomcatPath); if(tomcatPath!=null&&tomcatPath.length()>0){ int s = tomcatPath.lastIndexOf("classes"); tomcatPath = tomcatPath.substring(0,s ); int pos=tomcatPath.indexOf("file:"); if(pos>-1) tomcatPath=tomcatPath.substring(pos+5); } String sourceFileName=tomcatPath+"config/systemConfig.properties";//获取配置文件的地址 try{ sourceFileName=java.net.URLDecoder.decode(sourceFileName,"utf-8"); }catch(Exception e){throw new RuntimeException(e);} FileInputStream in; Properties prop = new Properties(); try { in = new FileInputStream(sourceFileName); prop.load(in); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return prop; } }