我需要一个文件配置的java代码
时间: 2023-02-20 21:04:01 浏览: 38
您好!我可以为您提供一个示例代码,帮助您读取配置文件。
以下是使用 Java 语言读取 properties 文件的示例代码:
```
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadConfigFile {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
String value = prop.getProperty("key");
System.out.println("Value: " + value);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
在上面的代码中,我们使用了 Properties 类来读取 properties 文件。首先,我们创建了一个 Properties 对象,然后通过 FileInputStream 读取 properties 文件。最后,我们使用 getProperty 方法读取配置文件中的键值对。
希望这段代码对您有所帮助!