使用Properties对象,加载存储在属性文件中的属性。
//n o w j a v a . c o m - 时代Java 提供 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class Main { public static void main(String[] args) throws Exception { File file = new File("properties.conf"); Properties properties = null; if (!file.exists()) { file.createNewFile(); } properties = new Properties(); properties.load(new FileInputStream("properties.conf")); boolean shouldWakeUp = false; int startCounter = 100; String shouldWakeUpProperty = properties.getProperty("ShouldWakeup"); shouldWakeUp = (shouldWakeUpProperty == null) ? false : Boolean .parseBoolean(shouldWakeUpProperty.trim()); /** from * n o w j a v a . c o m **/ String startCounterProperty = properties.getProperty("StartCounter"); startCounter = Integer.parseInt(startCounterProperty); String dateFormatStringProperty = properties.getProperty( "DateFormatString", "MMM dd yy"); System.out.println("Should Wake up? " + shouldWakeUp); System.out.println("Start Counter: " + startCounter); System.out.println("Date Format String:" + dateFormatStringProperty);