Java Properties Load from File load(String fileName)

Here you can find the source of load(String fileName)

Description

load

License

Apache License

Declaration

public static void load(String fileName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final Properties PROPERTIES = new Properties();

    public static void load(String fileName) {
        InputStream stream = getInputStream(fileName);
        try {//from  w ww .  j ava  2  s  .c  o  m
            if (stream == null) {
                throw new IllegalArgumentException("Unable to load: " + fileName);
            } else {
                PROPERTIES.load(stream);
                trimValues();
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to load: " + fileName, e);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new IllegalStateException("Unable to close properties file", e);
                }
            }
        }
    }

    private static InputStream getInputStream(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    }

    private static void trimValues() {
        for (String key : PROPERTIES.stringPropertyNames()) {
            String value = PROPERTIES.getProperty(key);
            String trimmedValue = value.trim();
            storeValue(key, trimmedValue);
        }
    }

    /**
     * @param key property's key
     * @return value for property
     * @throws IllegalArgumentException if no value is configured for this key.
     */
    public static String getProperty(String key) {
        if (!PROPERTIES.containsKey(key)) {
            throw new IllegalArgumentException("Unknown key: " + key);
        } else {
            return PROPERTIES.getProperty(key);
        }
    }

    private static void storeValue(String key, String trimmedValue) {
        PROPERTIES.put(key, trimmedValue);
    }
}

Related

  1. load(Properties props, File f)
  2. load(Properties props, File file)
  3. load(Properties props, String filename)
  4. load(String file)
  5. load(String file)
  6. load(String filename)
  7. load(String fileName)
  8. load(String filename)
  9. load(String filename)