Eclipseplugins
PropertiesUtils.java
1 package com.proalpha.pds.projconf.properties;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Properties;
8 
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 
12 final class PropertiesUtils {
13 
14  private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
15 
16  private PropertiesUtils() {
17  super();
18  }
19 
28  public static void storeProperties(Properties properties, File outputFile) throws IOException {
29 
30  if (!outputFile.exists()) {
31  File parentDir = outputFile.getParentFile();
32  if ((parentDir != null) && (!parentDir.exists())) {
33  parentDir.mkdirs();
34  }
35  boolean created = outputFile.createNewFile();
36 
37  // createNewFile returns false if the file already exists
38  if (!created)
39  logger.warn("The file {} already existed.", outputFile);
40  }
41 
42  try (FileOutputStream out = new FileOutputStream(outputFile);) {
43  properties.store(out, "");
44  }
45 
46  }
47 
56  public static Properties loadProperties(File file) throws IOException {
57 
58  Properties properties = new Properties();
59 
60  if (file == null) {
61  throw new IllegalArgumentException("Properties file is null.");
62  }
63 
64  if (!file.exists()) {
65  throw new IllegalArgumentException(
66  String.format("%s file does not exits or is null.", file.getAbsolutePath()));
67  }
68 
69  try (FileInputStream reader = new FileInputStream(file)) {
70  properties.load(reader);
71  }
72 
73  return properties;
74 
75  }
76 }