java怎么读取property文件

40次阅读
没有评论

共计 906 个字符,预计需要花费 3 分钟才能阅读完成。

在 Java 中,可以使用 java.util.Properties 类来读取 property 文件。
以下是读取 property 文件的步骤:

  1. 创建 Properties 对象:
Properties properties = new Properties();
  1. 使用 load() 方法加载 property 文件:
try {FileInputStream fileInputStream = new FileInputStream("path/to/your/file.properties");
properties.load(fileInputStream);
fileInputStream.close();} catch (IOException e) {e.printStackTrace();
}
  1. 使用 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 文件路径。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计906字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)