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 Map<String, String> load(String filename) 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Main {

    public static Map<String, String> load(String filename) {
        if (!filename.endsWith("properties"))
            filename += ".properties";
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
        return load(is);
    }//from ww  w  .j  a  v a2s  . co  m

    public static Map<String, String> load(File properties) {
        try {
            return load(new FileInputStream(properties));
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public static Map<String, String> load(InputStream is) {
        try {
            Properties prop = new Properties();
            prop.load(new InputStreamReader(is, "UTF-8"));
            Map<String, String> map = new HashMap<String, String>(prop.size(), 1.0f);
            for (Object k : prop.keySet()) {
                Object v = prop.get(k);
                String key = String.valueOf(k), value = String.valueOf(v);
                map.put(key, value);
            }
            return map;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (Exception ignore) {
                }
        }
    }
}

Related

  1. load(String file)
  2. load(String fileName)
  3. load(String filename)
  4. load(String fileName)
  5. load(String filename)
  6. load(String fileName)
  7. load(String path)
  8. load(String propertiesString)
  9. load(String resource)