java 读配置文件sqlurl=jdbc:mysql://localhost:3306/mydatabase sqluser=root sqlpwd=12345678 mfusername=test1 index=1,2,3 offset=50
时间: 2023-08-12 19:04:07 浏览: 168
JDBC:MySQL8.0.29驱动
5星 · 资源好评率100%
你可以使用 Java 的 Properties 类来读取配置文件中的键值对。以下是一个示例代码,假设配置文件名为 config.properties:
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadConfig {
public static void main(String[] args) {
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream("config.properties");
prop.load(fis);
fis.close();
String sqlUrl = prop.getProperty("sqlurl");
String sqlUser = prop.getProperty("sqluser");
String sqlPwd = prop.getProperty("sqlpwd");
String mfUsername = prop.getProperty("mfusername");
String[] indexArr = prop.getProperty("index").split(",");
int offset = Integer.parseInt(prop.getProperty("offset"));
System.out.println("sqlurl: " + sqlUrl);
System.out.println("sqluser: " + sqlUser);
System.out.println("sqlpwd: " + sqlPwd);
System.out.println("mfusername: " + mfUsername);
System.out.print("index: ");
for (String index : indexArr) {
System.out.print(index + " ");
}
System.out.println();
System.out.println("offset: " + offset);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
输出结果:
```
sqlurl: jdbc:mysql://localhost:3306/mydatabase
sqluser: root
sqlpwd: 12345678
mfusername: test1
index: 1 2 3
offset: 50
```
阅读全文