Skip to content

Commit

Permalink
✨ Add new prop utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarlett authored and avano committed Sep 11, 2023
1 parent aaea7b4 commit 91966a4
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package software.tnb.common.utils;

import org.apache.commons.io.FileUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -63,4 +66,17 @@ public static Properties fromFile(String filePath) {
}
return properties;
}

public static void setProperties(Path filePath, Map<String, String> props) {
final Properties properties = new Properties();
try (InputStream is = new FileInputStream(filePath.toFile())) {
properties.load(is);
props.entrySet().forEach(e -> properties.setProperty(e.getKey(), e.getValue()));
//recreate file
FileUtils.deleteQuietly(filePath.toFile());
IOUtils.writeFile(filePath, toString(properties));
} catch (IOException e) {
throw new RuntimeException("Unable to load properties from " + filePath, e);
}
}
}

0 comments on commit 91966a4

Please sign in to comment.