code.dreamsense.com

April 02, 2020

Last time we talked about creating prooperty files. Today, let's see how we read a property file.

First the properties file we'll be loading.

  greeting=Hello World

Now we load the file and output a value to the screen.

  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.Properties;

  public class Property {

    public static void main(String[] args) {
      try (InputStream input = new FileInputStream("file.properties")) {

        Properties prop = new Properties();

        prop.load(input);

        System.out.println(prop.getProperty("greeting"));

      } catch (IOException ex) {
          ex.printStackTrace();
      }
    }
  }

Kyle Eggleston
kyle@dreamsense.com
Copyright © 2003 - 2020 archive