java创建配置文件_idea 创建properties配置文件的步骤
时间: 2023-12-19 09:07:35 浏览: 43
好的,我来回答你的问题。
在 IntelliJ IDEA 中创建 properties 配置文件的步骤如下:
1. 在项目中的任意位置右键点击,选择 New -> File。
2. 在弹出的对话框中,输入文件名并在文件名后面加上 .properties 后缀,例如:config.properties。
3. 点击 OK 按钮,创建新的 properties 文件。
4. 在新创建的 properties 文件中,输入需要配置的属性和值,例如:
```
database.url=jdbc:mysql://localhost:3306/test
database.username=root
database.password=123456
```
5. 保存文件即可。
另外,如果你需要在 Java 代码中读取 properties 文件中的配置信息,可以使用 Properties 类的 load 方法进行加载,例如:
```java
Properties prop = new Properties();
InputStream input = new FileInputStream("config.properties");
prop.load(input);
String url = prop.getProperty("database.url");
String username = prop.getProperty("database.username");
String password = prop.getProperty("database.password");
```
注意:在使用 Properties 类的 load 方法加载 properties 文件时,需要处理可能抛出的异常,例如 FileNotFoundException 和 IOException。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)