java怎么读取property文件

在Java中,可以使用java.util.Properties类来读取property文件。

以下是读取property文件的步骤:

创建Properties对象:

Properties properties = new Properties();

使用load()方法加载property文件:

try {
FileInputStream fileInputStream = new FileInputStream("path/to/your/file.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

使用getProperty()方法获取属性值:

String value = properties.getProperty("key");

完整的示例代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertyFile {
public static void main(String[] args) {
Properties properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("path/to/your/file.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
String value = properties.getProperty("key");
System.out.println(value);
}
}

确保将"path/to/your/file.properties"替换为你实际的property文件路径。

阅读剩余
THE END