Read Java Property file

This time, I will introduce how to read Java Property file.

What is a Property file?

A Property file is a file that stores data with a pair of keys and values. It is the same as the Windows ini file. It is used as a file to save the setting values of the program. If you describe it in the Property file, you can change the behavior of the program without compiling, so it is better to set a value that may change later. For example, user ID and password.


The description format is described by connecting the key and value with "= (equal)". Specifically, it looks like the following.


In addition, the comment is displayed after the "# (sharp)" written at the beginning of the line.

#This line is a comment.
Key 1=value
Key 2=value#
Key 3#=value

In this case, the value corresponding to "Key 2" is "Value #". Also, the value corresponding to "Key 3 #" is "Value".

Read Property file

Use java.util.Properties to load the Property file. I think it is faster to see the actual program, so I will describe a concrete example.

public class PropertyUtil {

    private static final String INIT_FILE_PATH = "resourse/";
    private static final Properties properties;

    private PropertyUtil() throws Exception {

    static {
        properties = new Properties();
        try {
            properties.load(Files.newBufferedReader(Paths.get(INIT_FILE_PATH), StandardCharsets.UTF_8));
        } catch (IOException e) {
            //File reading failed
            System.out.println(String.format("Failed to read the file. file name:%s", INIT_FILE_PATH));

     *Get property value
     * @param key key
     * @return value
    public static String getProperty(final String key) {
        return getProperty(key, "");

     *Get property value
     * @param key key
     * @param defaultValue Default value
     * @Default value if return key does not exist
     *Value, if present
    public static String getProperty(final String key, final String defaultValue) {
        return properties.getProperty(key, defaultValue);

Use the Properties load function to load the Property file. load receives Reader as an argument, so use Reader that has read the Property file. The value of the loaded Property file can be obtained by specifying the key and using the getProperty function. Let's write a usage example.

test=Test value

    public static void main(String[] args) {
        System.out.println(PropertyUtil.getProperty("test")); //⇒ Test value

If you specify the key like this, you can get the corresponding value.

that's all.

Recommended Posts

Read Java Property file
[Java] [Android] Read ini file
Read Java properties file in C #
java file creation
Read xlsx file in Java with Selenium
[Java] File system operation
Read JSON in Java
Read a string in a PDF file with Java
java (split source file)
java core: chopped core file
Read Java HashMap source
[Java] Read the file in src / main / resources
<java> Read Zip file and convert directly to string
Read binary files in Java 1
Read standard input in Java
[Java] Create a temporary file
Read binary files in Java 2
Java beginners read Hello World
Java PropertyChangeListener detects property changes
How to read your own YAML file (*****. Yml) in Java
Easily read text files in Java (Java 11 & Java 7)
Text file placed in resources in Java cannot be read when jarted
Upload a file using Java HttpURLConnection
Read CSV in Java (Super CSV Annotation)
Read dump file with Docker MySQL
Run a batch file from Java
Unzip the zip file in Java
Log output to file in Java
Read the first 4 bytes of the Java class file and output CAFEBABE
EXCEL file update sample with JAVA
About file copy processing in Java
Read items containing commas in a CSV file without splitting (Java)
Sample to read and write LibreOffice Calc fods file in Java 2021
How to read log4j configuration file in Java project summarized in jar file Memo
Change the Swagger-ui read file. (Using AWS/Docker)
[Java] How to use the File class
Java for All! I read everyone's Java #minjava
What is the best file reading (Java)
[Read Effective Java] Chapter 2 Item 7 "Avoid Finalizers"
Why does Java call a file a class?
Read Felica using RC-S380 (PaSoRi) in Java
Java (exception handling, threading, collection, file IO)
Java: Place the ResourceBundle properties file anywhere
Sample to unzip gz file in Java
[Java8] Search the directory and get the file