java中如何读取properties文件呢?
下文笔者讲述java中读取properties文件的方法分享,如下所示:
其文件类型为“*.properties”
其文件内容为:"键=值或键:值"
在properties文件中我们可使用#或//为properties文件添加注释信息, 如
test.properties
java中读写properties的方法
操作properties的示例分享
properties文件简介
在java中properties文件是一种配置文件,它常用于配置信息的存放,其文件类型为“*.properties”
其文件内容为:"键=值或键:值"
在properties文件中我们可使用#或//为properties文件添加注释信息, 如
test.properties
################################# # 配置文件 # 2021-10-4 ################################# # 配置信息 userName=Java265.com #################################

java中读写properties的方法
实现思路:
1.将properties文件加载为inputStream
2.使用Properties类并借助inputStream生成properties对象
3.对新生成对象操作,即可操作properties中的信息
例:操作properties的示例分享
package com.java265.other;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test {
/**
* java265.com 操作properties文件的示例分享
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(new File("D:\\\\test.properties"));
Properties properties = new Properties();
properties.load(inputStream);
String userName = properties.getProperty("userName");
System.out.println(userName);
System.out.println("=============");
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


