java-ini-parser helps you parse ini files in java.
Add a dependency to com.github.vincentrussell:java-ini-parser
.
<dependency>
<groupId>com.github.vincentrussell</groupId>
<artifactId>java-ini-parser</artifactId>
<version>1.7</version>
</dependency>
- JDK 1.8 or higher
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini");
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Collection<String> sections = ini.getSections();
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Collection<String> keys = ini.getKeys("FTPS")
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Object value = ini.getValue("String", "string")
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
boolean has = ini.hasKey("String", "string")
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Map<String, Object> value = ini.getSection("FTP")
Be careful! Precision can be lost here.
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
int value = ini.getValue("Numbers", "long", int.class)
Ini ini = new Ini();
ini.putValue("section", "key", "value")
Ini ini = new Ini();
Ini ini2 = new Ini();
ini2.putValue("Char", "charKey", "b");
ini2.putValue("Char", "characterKey", "z");
ini2.putValue("String", "string", "Hello");
ini.putValues("Char", ini2.getSection("Char"));
Ini ini = new Ini();
Ini ini2 = new Ini();
ini2.putValue("String", "user2", "Henry2");
ini.merge(ini2);
Ini ini = new Ini();
ini.putValue("section", "key", "value")
ini.store(new FileWriter("/tmp/file.ini"), "some comments at the top of the file");
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Object value = ini.removeSectionKey("String", "string")
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Object value = ini.removeSection("String")
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Map<String, Object> result = ini.getSectionWithKeysWithPrefix("Sample", "my.port.");
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Map<String, Object> result = ini.getSectionWithKeysWithRegex("Sample", "^my\\.port\\.[\\d]{1}");
Ini ini = new Ini();
ini.load(new FileInputStream("samples/sample2.ini"));
Map<String, Object> result = ini.getSectionWithKeysThatMatchFunction("Sample", entry -> Double.class.isInstance(entry.getValue()));
[group]
key=value
multilineKey = \
this \
is \
a \
multi-line \
value
anotherKey = value
string interpolation support from variables defined in ini section, system properties or environment variables
[section]
variable=some value
sysProperty=wouldn't you like to know that ${some.sys.property}
varKey=value is ${variable}
envVarKey=value is ${ENV_VAR1}
1.7 (2024-10-05)
Enhancements:
- N/A
Bugs:
- Fixed null pointer exception when using getValue with casting
1.6 (2023-10-28)
Enhancements:
- added the ability to merge two Ini files
- added the ability to put an entire map into an ini section (merge section)
Bugs:
- Fixed issues with writing out multiline comments
1.5 (2023-03-10)
Enhancements:
- added hasKey function
Bugs:
- Booleans not handled properly when calling ini.getValue("Boolean", "key", Boolean.class)
- Characters not handled properly when calling ini.getValue("Char", "charKey", char.class)
1.4 (2022-12-29)
Enhancements:
- Internal ini structure is now backed by LinkedHashMap instead of Hashmap to maintain insert order
- created Map<String, Object> getSectionWithKeysThatMatchFunction(String section, Predicate<Map.Entry<String, Object>> filter) method
- multiline support with ending lines with '\'
- string interpolation support
Bugs:
- Escaped semicolons and pound signs not working properly
1.3 (2022-08-21)
Enhancements:
- Support for inline comments with ; and #
- Support for special characters that can be found in ini files like \b,\f,\r\t,etc
1.2 (2022-07-20)
Enhancements:
- Upgraded commons-io
1.1 (2021-09-06)
Enhancements:
- Created getSectionWithKeysWithPrefix and getSectionWithKeysWithRegex
- Added the ability to remove sections and remove values from ini.
1.0 (2021-08-20)
Bugs:
- N/A