Java获取配置文件信息ResourceBundle

本文介绍了如何在Java中使用ResourceBundle来读取和管理配置文件,特别是abc.properties文件。通过简单的步骤,讲解了配置文件的创建和设置,以及如何在代码中获取并使用配置文件中的KEY-VALUE对信息。
摘要由CSDN通过智能技术生成

首先准备好一个配置文件abc.properties,根据项目类型放在对应的目录下;

文件内,以KEY=VALUE格式写好你的内容。

abc.test=ddddddddddd

在Java中
private sta
Java 获取配置文件信息有很多方法,常用的有以下几种: 1. 使用 java.util.Properties 类。Properties 类可以用来读取 Java配置文件,通常是以 .properties 为后缀的文件。使用 Properties 类可以方便地读取配置文件信息。例如: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class Main { public static void main(String[] args) throws IOException { Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); String name = prop.getProperty("name"); int age = Integer.parseInt(prop.getProperty("age")); } } ``` 2. 使用 java.util.ResourceBundle 类。ResourceBundle 类可以用来读取多种语言的配置文件,通常是以 .properties 为后缀的文件。例如: ```java import java.util.ResourceBundle; public class Main { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config"); String name = rb.getString("name"); int age = Integer.parseInt(rb.getString("age")); } } ``` 3. 使用 java.util.prefs.Preferences 类。Preferences 类可以用来读取操作系统的配置信息,例如 Windows 的注册表。例如: ```java import java.util.prefs.Preferences; public class Main { public static void main(String[] args) { Preferences prefs = Preferences.userNodeForPackage(Main.class); String name = prefs.get("name", ""); int age = prefs.getInt("age", 0); } } ``` 4. 使用 java.util.Scanner 类。Scanner 类可以用来读取任意类型的文件,包括配置文件。例如: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(new
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值