Skip to content

Instantly share code, notes, and snippets.

@myamamic
Created December 11, 2012 12:31
Show Gist options
  • Save myamamic/4258229 to your computer and use it in GitHub Desktop.
Save myamamic/4258229 to your computer and use it in GitHub Desktop.
[Java][Android] 簡単なコンフィグファイルパーサー
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ConfigFileParser {
private final static String TAG = "ConfigFileParser";
private final static String DEFAULT_SEPARATOR = "=";
private String mConfigFilePath = null;
private String mSeparator = DEFAULT_SEPARATOR;
/**
* @param configFilePath Specify config file.
*/
public ConfigFileParser(String configFilePath) {
mConfigFilePath = configFilePath;
}
/**
* @param configFilePath Specify config file.
* @param separator this is a charactor to devide parameter and value.
* ex) PARAM1=VALUE1 # '=' is a separator.
*/
public ConfigFileParser(String configFilePath, String separator) {
mConfigFilePath = configFilePath;
mSeparator = separator;
}
/**
* @param param parameter name.
* @param defaultValue If can not get the parameter, use this value.
* @return Int value of parameter. If can not get the value, it is defaultValue.
*/
public int getConfigParameterInt(String param, int defaultValue) {
if ((param == null)||(param.length() <= 0)) {
return defaultValue;
}
String str = this.getConfigParameter(param);
if (str != null) {
return Integer.parseInt(str);
} else {
return defaultValue;
}
}
/**
* @param param parameter name.
* @param defaultValue If can not get the parameter, use this value.
* @return String value of parameter. If can not get the value, it is defaultValue.
*/
public String getConfigParameterString(String param, String defaultValue) {
if ((param == null)||(param.length() <= 0)) {
return defaultValue;
}
String str = this.getConfigParameter(param);
if (str != null) {
return str;
} else {
return defaultValue;
}
}
/**
* @param param parameter name.
* @return Int value of parameter.
*/
public boolean setConfigParameterInt(String param, int value) {
if ((param == null)||(param.length() <= 0)) {
return false;
}
return setConfigParameter(param, String.valueOf(value));
}
/**
* @param param parameter name.
* @return String value of parameter.
*/
public boolean setConfigParameterString(String param, String value) {
if ((param == null)||(param.length() <= 0)) {
return false;
}
return setConfigParameter(param, value);
}
private String getConfigParameter(String param) {
File file = new File(mConfigFilePath);
FileReader input = null;
BufferedReader reader = null;
String parseLine = null;
try {
input = new FileReader(file);
reader = new BufferedReader(input);
try {
while (null != (parseLine = reader.readLine())) {
if (parseLine.startsWith(param)) {
break;
}
}
if ((parseLine == null)||(parseLine.length() <= 0)) {
return null;
}
int separator = parseLine.indexOf(mSeparator);
if (separator == -1) {
Log.w(TAG, "Parse ERROR: " + param + " line has no " + mSeparator + ".");
return null;
}
return parseLine.substring(separator+1);
} catch (IOException ioe) {
Log.w(TAG, "IO exception occured.");
return null;
} catch (NumberFormatException nfe) {
Log.wtf(TAG, "unexpected data in status file, will start from begining");
return null;
}
} catch (FileNotFoundException ex) {
Log.w(TAG, "Config file not found.");
return null;
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ioe) { /* Do nothing. */ }
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) { /* Do nothing. */ }
}
}
private boolean setConfigParameter(String param, String value) {
File file = new File(mConfigFilePath);
File tmpFile = new File(mConfigFilePath + ".tmp");
// Create temporary config file.
if (!YamaUtil.CopyFile(file, tmpFile)) {
return false;
}
FileReader input = null;
BufferedReader reader = null;
String parseLine = null;
FileWriter output = null;
try {
String lineSeparator = System.getProperty("line.separator");
input = new FileReader(tmpFile);
reader = new BufferedReader(input);
output = new FileWriter(file);
try {
while (null != (parseLine = reader.readLine())) {
if (parseLine.startsWith(param)) {
String newLine = param + mSeparator + value;
output.write(newLine + lineSeparator);
continue;
}
output.write(parseLine + lineSeparator);
}
return true;
} catch (IOException ioe) {
Log.w(TAG, "IO exception occured.");
return false;
}
} catch (FileNotFoundException ex) {
Log.w(TAG, "Config file not found.");
return false;
} catch (IOException ioe) {
Log.w(TAG, "IO exception occured.");
return false;
} finally {
tmpFile.delete();
try {
if (reader != null) {
reader.close();
}
} catch (IOException ioe) { /* Do nothing. */ }
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) { /* Do nothing. */ }
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) { /* Do nothing. */ }
}
}
}
@myamamic
Copy link
Author

Stringとintのみサポート

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment