java解析cfg.properties属性文件
1、第一种:主要是通过class.getClassLoader().getResourceAsStream1、是实现获取在classpath路径下的资源文件的输入流为什么是艘早祓胂classpath而不是src,因为当web项目运行时,IDE编译器会把src下的一些资源文件移至WEB-INF/classes,classPath目录其实就是这个classes目录。这个目录下放的一般是web项目运行时的class文件、资源文件(xml,properties...);2、主要代码:import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class PropertiesUtils {/*** 根据key获取cfg.properties的值** @param key* @return*/public static String getCfgPropertiesValue(String key) {Properties pro = new Properties();InputStream is = null;try {is = PropertiesUtils.class.getClassLoader().getResourceAsStream("CodeMapping.properties");System.out.println();// 读取属性文件pro.load(is);} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}return pro.getProperty(key);}}3、main方法测试结果public class Test5 { public static void main(String[] args) { System.out.println("name:" + PropertiesUtils.getCfgPropertiesValue("name")); }}
2、第二种:通过class.getResourceAsStream(Stringname)获取。import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Test5 { public static void main(String[] args) { System.out.println("name:" + getCfgPropertiesValue("name")); } /** * 根据key获取CodeMapping.properties的值 * @param key * @return */ public static String getCfgPropertiesValue(String key) { Properties pro = new Properties(); InputStream is = null; try { is = Test5.class .getResourceAsStream("/CodeMapping.properties"); System.out.println(); // 读取属性文件 pro.load(is); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } return pro.getProperty(key); }}
3、第三种:使用绝对路径解析。1、这种使用绝对路径的方式需要你能够准确提供位置2具体代码如下所莲镘拎扇示:import java.io.FileInputStre锾攒揉敫am;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class Test5 { public static void main(String[] args) { System.out .println("name:" + getPropertiesValue( "D:/Workspaces/MyEclipse 10/servlet/src/CodeMapping.properties", "name")); } public static String getPropertiesValue(String filePath, String key) { Properties pro = new Properties(); InputStream in = null; try { // 读取属性文件 in = new FileInputStream(filePath); pro.load(in); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return pro.getProperty(key); }}
4、springboot打的jar运行包获取当签jar包的上一级文件夹模目录String filePath = System.getProperty("user.dir");String realFilePath = filePath.substring(0, filePath.lastIndexOf("\\") + 1);/2、借助于系统环境变量比如在配置java的环境中同样配置一个key==val的环境变量。key相当于变量名,val也就是地址。在springboot中可以使用${key}获取地址。
5、通过java工具类:java.util.ResourceBundle1、可以解析普通java项目src下的属性文件或者是springboot中resource中的属性文件。2、解析代码如下private static ResourceBundle resourceBundle ;static{ resourceBundle = ResourceBundle.getBundle("bank_merchant_config"); String cafile = resourceBundle.getString("cafile"); String store_password = resourceBundle.getString("store_password"); InputStream inputStream = null; try{ inputStream = new FileInputStream(cafile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } char pass[] = store_password != null ? store_password.toCharArray() : null; try{ keyStore = KeyStore.getInstance("JKS"); keyStore.load(inputStream, pass); }catch(Exception e) { e.printStackTrace(); }}