Reading Property Files - Java File Path IO

Java examples for File Path IO:Property Files

Introduction

Using the Properties object, load properties stored within the properties file.

Demo Code

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();/* w  ww.  ja va 2s . co m*/
    }
    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());

    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);

    // setting property
    properties.setProperty("StartCounter", "250");
    properties.store(new FileOutputStream("properties.conf"),
        "Properties Description");
    properties.list(System.out);
  }
}

Related Tutorials