View Javadoc
1   package de.fhdw.wtf.persistence.utils;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.util.Enumeration;
7   import java.util.HashMap;
8   import java.util.MissingResourceException;
9   import java.util.Properties;
10  
11  /**
12   * This PropertiesReader tries to read properties from a file in a given directory. The content of the file should be
13   * seperated by "key1=value1".
14   * 
15   * @author HFW413hy
16   *
17   */
18  public class PropertiesReaderFile implements PropertiesReader {
19  	
20  	/**
21  	 * Stores the internal key-value-pairs.
22  	 */
23  	private final HashMap<String, String> propValues = new HashMap<>();
24  	
25  	@Override
26  	public String getProperty(final String key) {
27  		if (!this.propValues.containsKey(key)) {
28  			return "";
29  		}
30  		return this.propValues.get(key);
31  	}
32  	
33  	@Override
34  	public void initialize(final String propFile) throws MissingResourceException {
35  		final File f = new File(propFile);
36  		if (!f.exists()) {
37  			throw new MissingResourceException("", "", "");
38  		}
39  		
40  		final Properties props = new Properties();
41  		try (final FileInputStream inputStream = new FileInputStream(new File(propFile))) {
42  			props.load(inputStream);
43  		} catch (final IOException e) {
44  			throw new MissingResourceException("", "", "");
45  		}
46  		
47  		final Enumeration<Object> keys = props.keys();
48  		while (keys.hasMoreElements()) {
49  			final String key = (String) keys.nextElement();
50  			final String value = props.getProperty(key);
51  			this.propValues.put(key, value);
52  		}
53  	}
54  }