code.dreamsense.com

March 28, 2020

Last time we talked about writing text files, today we're talking about property files.

A property file is a map of keys and values. It's stored in a file.

We can create a property file by doing the following:

  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Properties;

  public class Property {

    public static void main(String[] args)
      throws FileNotFoundException, IOException {
      Properties props = new Properties();
      props.put("one", "1");
      props.put("two", "2");
      props.store(new FileOutputStream("test.properties"),
        "any comment here");
    }

  }
Running the above code, we get the following test.properties file:
  #any comment here
  #Sat Mar 28 12:42:27 MDT 2020
  two=2
  one=1

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