共計 722 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 Properties
類來讀取配置文件。下面是一個簡單的示例:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadConfigFile {public static void main(String[] args) {Properties properties = new Properties();
FileInputStream fis = null;
try {// 加載配置文件
fis = new FileInputStream("config.properties");
properties.load(fis);
// 讀取配置項的值
String value1 = properties.getProperty("key1");
String value2 = properties.getProperty("key2");
System.out.println("key1 = " + value1);
System.out.println("key2 = " + value2);
} catch (IOException e) {e.printStackTrace();
} finally {if (fis != null) {try {fis.close();
} catch (IOException e) {e.printStackTrace();
}
}
}
}
}
上述代碼假設配置文件名為config.properties
,文件內容如下:
key1=value1
key2=value2
在文件所在的目錄下執行上述代碼,就可以讀取配置文件中的值了。
丸趣 TV 網 – 提供最優質的資源集合!
正文完