java解析cfg.properties属性文件

2025-11-05 04:58:43

1、第一种:主要是通过class.getClassLoader().getResourceAsStream

1、是实现获取在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"));

}

}

java解析cfg.properties属性文件

java解析cfg.properties属性文件

2、第二种:通过class.getResourceAsStream(String name)获取。

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);

}

}

java解析cfg.properties属性文件

java解析cfg.properties属性文件

3、第三种:使用绝对路径解析。

1、这种使用绝对路径的方式需要你能够准确提供位置

2具体代码如下所示:

import java.io.FileInputStream;

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);

}

}

java解析cfg.properties属性文件

java解析cfg.properties属性文件

java解析cfg.properties属性文件

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.ResourceBundle

1、可以解析普通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();

 

   }

 

}

1、第一步:javaweb项目打包总结。

1、普通javaweb项目中的编译会将内容编译在WEB-INF\classes

1、springboot项目本地开发会将src/main/java和resources目录下的内容编译到target\classes下。打成jar包之后:excelimport-0.0.1-SNAPSHOT.jar。目录为:excelimport-0.0.1-SNAPSHOT.jar\BOOT-INF\classes

java解析cfg.properties属性文件

java解析cfg.properties属性文件

java解析cfg.properties属性文件

2、第二步:class.getClassLoader().getResourceAsStream(file)和class.getResourceAsStream(file)比较。

1、都是实现获取在classpath路径下的资源文件的输入流。

2、为什么是classpath而不是src,因为当web项目运行时,IDE编译器会把src下的一些资源文件移至WEB-INF/classes,classPath目录其实就是这个classes目录。这个目录下放的一般是web项目运行时的class文件、资源文件(xml,properties...);

3、class.getClassLoader().getResourceAsStream(file)相当于直接在根目录classes查找文件和Thread.currentThread().getContextClassLoader().getResourceAsStream一样,文件不在根目录classes下无法读取。

InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("CodeMapping.properties");

4、class.getResourceAsStream(file)定位相对于文件夹classes获取下一级内容需要加“/”到具体目录

Test5.class.getResourceAsStream("/CodeMapping.properties");

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢