注意,.properties文件放在根目录即可。
String classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); //获取根目录 String fileName = classpath + "truths.properties"; //配置文件名称 Properties p = new Properties(); FileInputStream fis; try { fis = new FileInputStream(fileName); p.load(fis); //初始化 return p.getProperty("server"); //key名称 } catch (Exception e) { e.printStackTrace(); }
防止中文乱码以及防止%20 发生惨案.
String classpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); classpath = URLDecoder.decode(classpath,"UTF-8 "); //liunx 环境下,防止" " 转化为%20 而路径报错 String fileName = classpath + "truths.properties"; Properties p = new Properties(); FileInputStream fis = new FileInputStream(fileName); //防止中文乱码/. BufferedReader bf = new BufferedReader(new InputStreamReader(fis)); p.load(bf); p.getProperty("server");
简单暴力. 直接使用.
http://blog.csdn.net/buster2014/article/details/42045225
http://www.lai18.com/content/2659688.html