One way to save the settings to a file in Java is to save it as a .properties file. However, there are times when you want to use an .ini file, such as recreating an app created in C ++ in Java (or rather, during this time). So, let's write a process to read it.
It seems that .ini files do not have a strict format, so this time we will target the following formats.
IniFileLoader.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
*Loader to load IniFile
*/
public class IniFileLoader {
// HashMap<section, HashMap<key, String>>To
private HashMap<String, HashMap<String, String>> mDataMap;
//File Path
private String mFilePath;
//Is load completed?
private boolean mIsLoaded = false;
/**
*Load the specified file
*
* @param filePath File path
* @return Whether the load was successful
*/
public boolean load(String filePath) {
mFilePath = filePath;
return loadProcess(filePath);
}
/**
*Reload the file you last tried to load
*
* @return Whether the load was successful
*/
public boolean reload() {
return isEmpty(mFilePath) && loadProcess(mFilePath);
}
private boolean loadProcess(String filePath) {
mIsLoaded = false;
mDataMap = new HashMap<>();
try {
FileReader fileReader = new FileReader(new File(filePath));
BufferedReader br = new BufferedReader(fileReader);
String line = br.readLine();
//Section name
String section = null;
//Key value
HashMap<String, String> map = new HashMap<>();
while (line != null) {
//Remove whitespace at the beginning and end of lines
line = line.trim();
//Blank line
if (isEmpty(line)) {
// no process
}
//Comment line
else if (line.charAt(0) == '#') {
// no process
}
//Section line
else if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') {
section = line.substring(1, line.length() - 1);
map = new HashMap<>();
}
//Parameter line
else if (line.length() >= 3 && line.contains("=") && line.length() > line.indexOf("=") + 1) {
String key = line.substring(0, line.indexOf("="));
String value = line.substring(line.indexOf("=") + 1);
map.put(key, value);
mDataMap.put(section, map);
}
line = br.readLine();
}
br.close();
} catch (IOException e) {
return false;
}
mIsLoaded = true;
return true;
}
/**
*Read the result(section, (key, value))of{@link HashMap}Return with
*
* @return The result of reading{@link HashMap}
*/
public HashMap<String, HashMap<String, IniItem>> getAllDataMap() {
if (mIsLoaded) {
return mDataMap;
}
return null;
}
/**
*The specified section of the read result(key, value)of{@link Map}Return with
*
* @param section Section specification
* @return The specified section of the read result
*/
public Map<String, String> getSectionDataMap(String section) {
if (mIsLoaded) {
return mDataMap.get(section);
}
return null;
}
/**
*Returns the value of the specified section, specified key
*
* @param section Section specification
* @param key key specification
* @return Specified section, specified key value
*/
public String getValue(String section, String key) {
if (mIsLoaded) {
HashMap<String, String> map = mDataMap.get(section);
if (map != null) {
return map.get(key);
}
}
return null;
}
/**
*Returns whether the specified section is in the read result
*
* @param section Section specification
* @return if it exists{@code true}
*/
public boolean containsSection(String section) {
if (mIsLoaded) {
return mDataMap.containsKey(section);
}
return false;
}
/**
*Returns whether the specified key in the specified section is in the read result
*
* @param section Section specification
* @param key key specification
* @return if it exists{@code true}
*/
public boolean containsKey(String section, String key) {
if (mIsLoaded) {
HashMap<String, IniItem> map = mDataMap.get(section);
return map != null && map.containsKey(key);
}
return false;
}
/**
* {@code String}Determine if is empty
*
* @param str Judgment target
* @return {@code String}If is empty{@code true}
*/
private boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}
You just read it line by line and make a judgment.
https://github.com/entan05/IniFileController
I made it into a library. For the time being, it can also be used on Android.
[Java] [Sample code] I made my own INI file read / write library | http://javasampleokiba.blog.fc2.com/blog-entry-27.html
date | Contents |
---|---|
2018/03/24 | Post |
Recommended Posts