java测试类读取不到配置文件_Java读取.properties配置文件的几种方式

Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配置文件就比较容易。

介绍几种读取方式:

1、基于ClassLoder读取配置文件

注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。

Properties properties = new Properties();

// 使用ClassLoader加载properties配置文件生成对应的输入流

InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");

// 使用properties对象加载输入流

properties.load(in);

//获取key对应的value值

properties.getProperty(String key);

2、基于 InputStream 读取配置文件

注意:该方式的优点在于可以读取任意路径下的配置文件

Properties properties = new Properties();

// 使用InPutStream流读取properties文件

BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/config.properties"));

properties.load(bufferedReader);

// 获取key对应的value值

properties.getProperty(String key);

3、通过 java.util.ResourceBundle 类来读取,这种方式比使用 Properties 要方便一些

1>通过 ResourceBundle.getBundle() 静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可

properties.getProperty(String key);

//config为属性文件名,放在包com.test.config下,如果是放在src下,直接用config即可

ResourceBundle resource = ResourceBundle.getBundle("com/test/config/config");

String key = resource.getString("keyWord");

2>从 InputStream 中读取,获取 InputStream 的方法和上面一样,不再赘述

ResourceBundle resource = new PropertyResourceBundle(inStream);

注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取)或com/test/config/config(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用config.properties

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,您可以使用注解来读取配置文件中的数据。使用注解的好处是可以将数据注入到属性中,从而方便地在代码中使用这些数据。 以下是一个简单的例子,演示如何使用注解来读取配置文件中的数据: 首先,创建一个配置文件`config.properties`,并添加以下内容: ``` database=mysql dbuser=root dbpassword=123456 ``` 然后,创建一个`Config.java`,并添加以下代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.util.Properties; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @interface ConfigProperty { String value() default ""; } public class Config { @ConfigProperty("database") private String database; @ConfigProperty("dbuser") private String dbUser; @ConfigProperty("dbpassword") private String dbPassword; public Config() { readConfig(); } private void readConfig() { Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("config.properties"); prop.load(input); for (Field field : this.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(ConfigProperty.class)) { ConfigProperty annotation = field.getAnnotation(ConfigProperty.class); String propertyName = annotation.value().isEmpty() ? field.getName() : annotation.value(); String propertyValue = prop.getProperty(propertyName); field.setAccessible(true); field.set(this, propertyValue); } } } catch (IOException | IllegalAccessException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String getDatabase() { return database; } public String getDbUser() { return dbUser; } public String getDbPassword() { return dbPassword; } } ``` 在这个例子中,我们定义了一个注解`@ConfigProperty`,它可以用于属性上。注解有一个属性`value`,可以用来指定配置文件中的属性名。如果未指定,则默认使用属性名作为配置文件中的属性名。 我们还定义了一个`readConfig()`方法,用于读取配置文件中的数据并将其注入到属性中。在这个方法中,我们使用`Properties`来加载配置文件。然后,我们遍历中的所有属性,如果属性上有`@ConfigProperty`注解,则使用反射机制将属性设置为配置文件中的值。 最后,我们可以创建一个`Main.java`,来测试`Config.java`: ```java public class Main { public static void main(String[] args) { Config config = new Config(); System.out.println("Database: " + config.getDatabase()); System.out.println("DB User: " + config.getDbUser()); System.out.println("DB Password: " + config.getDbPassword()); } } ``` 在这个例子中,我们创建了一个`Config`对象,并打印出从配置文件读取的数据库信息。 当我们运行`Main.java`时,输出如下: ``` Database: mysql DB User: root DB Password: 123456 ``` 这表明我们已成功从配置文件读取了数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值