如果我将属性文件声明为config,java如何从文件夹外部读取属性文件。性质 1 周,4 日 Questions & Answers 348 我创建了属性文件,比如config.properties。现在我想知道如何从java中的属性文件中读取参数值的过程 使用jdk1。月食中的7
# 1 楼答案 请参阅下面的代码以读取配置。属性文件。属性文件可以是文件系统中的任何位置。必须为属性文件提供正确的绝对路径 import java.io.*; import java.util.Properties; class ConfigReader { public static void main(String args[]) { try { InputStream inputstream; Properties prop = new Properties(); inputstream = new FileInputStream("pathtofile\\config.properties"); if(inputstream!=null) { prop.load(inputstream); String property1 = prop.getProperty("property1"); String property2 = prop.getProperty("property2"); System.out.println(property1+"\n"+property2); } else { System.out.println("File not found"); } } catch(Exception e) { System.out.println("Error"); } } }
# 1 楼答案
请参阅下面的代码以读取配置。属性文件。属性文件可以是文件系统中的任何位置。必须为属性文件提供正确的绝对路径