JAVA服务器将配置文件中多个用户名和密码依次输出
时间: 2024-03-22 17:40:00 浏览: 15
您可以使用Java中的Properties类来读取配置文件中的用户名和密码,并使用迭代器(Iterator)来遍历所有的键值对并输出。
示例代码如下:
```java
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;
public class ConfigFileReader {
public static void main(String[] args) {
try {
// 读取配置文件
FileInputStream fileInputStream = new FileInputStream("config.properties");
Properties properties = new Properties();
properties.load(fileInputStream);
// 获取所有的键值对
Iterator<String> iterator = properties.stringPropertyNames().iterator();
// 遍历并输出所有的用户名和密码
while (iterator.hasNext()) {
String key = iterator.next();
String value = properties.getProperty(key);
System.out.println("Username: " + key + " Password: " + value);
}
fileInputStream.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
在上述代码中,我们首先使用FileInputStream类和Properties类来读取配置文件中的键值对。接着,我们使用Iterator来遍历所有的键值对,并输出它们的用户名和密码。最后,我们关闭文件输入流。