java中读取properties有哪些方式呢?
在日常开发中,我们通常将properties放入到指定位置,那么java程序如何读取配置文件呢?下文笔者将一一道来,如下所示
读取properties的实现思路
读取Properties的思路
1.使用`java.util.Properties` 类:
- 从文件系统或类路径读取。
- 简单易用,适用于大多数场景。
2.使用`java.util.ResourceBundle`类:
- 适用于国际化和本地化。
- 文件需要放在类路径下。
3.使用 Java NIO:
- 提供更现代的文件操作方式。
- 适用于需要更高级文件操作的场景。
4.使用 Apache Commons Configuration:
- 功能强大,支持多种配置文件格式。
- 需要添加额外的依赖。
5.使用 Spring Framework:
- 适用于使用 Spring 框架的应用。
- 提供强大的配置管理功能
1.使用`java.util.Properties` 类
`java.util.Properties`类 是Java 标准库中用于处理`properties`文件的类例
1.从文件系统读取`properties`文件
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) {
Properties properties = new Properties();
try (FileInputStream fis = new FileInputStream("config.properties")) {
properties.load(fis);
String value = properties.getProperty("key");
System.out.println("Value: " + value);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.从类路径读取`properties` 文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) {
Properties properties = new Properties();
try (InputStream is = PropertiesExample.class.getClassLoader().getResourceAsStream("config.properties")) {
if (is != null) {
properties.load(is);
String value = properties.getProperty("key");
System.out.println("Value: " + value);
} else {
System.out.println("Properties file not found!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.使用`java.util.ResourceBundle` 类
`ResourceBundle` 类用于国际化和本地化 也可以用来读取 `properties` 文件。例
基本用法
1.创建`properties`文件
假设有一个 `config.properties` 文件:
key=value
2.使用`ResourceBundle`读取
import java.util.ResourceBundle;
public class ResourceBundleExample {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("config");
String value = bundle.getString("key");
System.out.println("Value: " + value);
}
}
注意事项: -`ResourceBundle`会自动查找`config.properties`文件 不需要指定文件扩展名 -文件需要放在类路径下
3.使用`java.nio.file.Files`和 `java.nio.file.Paths`
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
public class NIOPropertiesExample {
public static void main(String[] args) {
Properties properties = new Properties();
try {
properties.load(Files.newInputStream(Paths.get("config.properties")));
String value = properties.getProperty("key");
System.out.println("Value: " + value);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.使用 Apache Commons Configuration
Apache Commons Configuration 是一个功能强大的库
用于处理各种配置文件
包括 `properties` 文件
例
添加依赖
如果你使用 Maven,可以在 `pom.xml` 中添加以下依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
基本用法
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
public class CommonsConfigExample {
public static void main(String[] args) {
Parameters params = new Parameters();
PropertiesConfiguration config = null;
try {
config = new PropertiesConfiguration.Builder("config.properties")
.configure(params.properties()
.setFileName("config.properties"))
.getConfiguration();
String value = config.getString("key");
System.out.println("Value: " + value);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
5.使用Spring Framework
使用Spring框架
可利用Spring提供配置管理功能
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
例
1.创建 `properties` 文件
假设有一个 `config.properties` 文件
key=value
2.使用 `@PropertySource`注解读取
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:config.properties")
public class ConfigProperties {
@Value("${key}")
private String key;
public String getKey() {
return key;
}
}
3.使用 `@Autowired` 注入配置
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
ConfigProperties configProperties = context.getBean(ConfigProperties.class);
System.out.println("Value: " + configProperties.getKey());
context.close();
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


