JAVA读取ini文件的一个类,存着以备后用!
2010-05-12 11:33
387 查看
package com.qiao.java;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Properties;
import org.jboss.util.Primitives;
import com.yamu.basic.StringUtils;
public class IniReader {
protected HashMap sections = new LinkedHashMap();
private transient String currentSecion;
private transient Properties current;
private final String filePath=ConfigUtil.getInstance().getValue("config_ini_filepath");
public static IniReader iniFile=new IniReader();
public static IniReader getInstance(){
return iniFile;
}
private IniReader() {}
private void read() throws IOException {
// modifyed by brmrk
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
reader.close();
}
/**
* 描述:处理读取ini文件的每一行
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:53:36
*/
protected void parseLine(String line) {
line = line.trim();
if (line.matches("//[.*//]")) {
currentSecion = line.replaceFirst("//[(.*)//]", "$1");
current = new Properties();
} else if (line.matches(".*=.*")) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name.trim(), value.trim());
sections.put(currentSecion, current);
}
}
/**
* 描述:根据传入的片段名称和值的名字获取对应的数值
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:09
*/
public String getValue(String section, String name) throws IOException {
System.out.println("section="+section+"name="+name);
this.read();
Properties p = (Properties) sections.get(section);
if (p == null) {
return null;
}
String value = p.getProperty(name);
System.out.println("-------------------------get value="+value);
return value;
}
/**
*
* 描述:根据片段名字,值的名字,值的数值进行更新
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:40
*/
public boolean changeProperty(String sectionName,String key,String value) throws IOException,IllegalArgumentException{
if(!StringUtils.isEmpty(key)){
return false;
}else{
this.read();//重新读取文件到内存中
Properties p=(Properties)sections.get(sectionName);
if(null == p){
throw new IllegalArgumentException("sectionName参数出错");
}
p.setProperty(key, value);
this.writeProperty();
return true;
}
}
/**
*
* 描述:将修改后的值写入配置文件
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:22
*/
private void writeProperty() throws IOException{
PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
try {
StringBuilder builder=new StringBuilder();
Iterator<String> iterator=sections.keySet().iterator();
while(iterator.hasNext()){
String sectionName=iterator.next();
Properties p=(Properties)sections.get(sectionName);
builder.append("/n").append("/n").append("[").append(sectionName).append("]");
builder.append(getPropertiesString(p));
}
writer.write(builder.toString().trim());
writer.flush();
writer.close();
} finally{
if(writer != null){
writer.close();
}
}
}
/**
*
* 描述:获取组织好的properties文件的字符串表示
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:49
*/
private String getPropertiesString(Properties p){
StringBuilder builder=new StringBuilder();
Iterator iterator=p.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
builder.append("/n").append(key).append("=").append(p.get(key));
}
return builder.toString();
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Properties;
import org.jboss.util.Primitives;
import com.yamu.basic.StringUtils;
public class IniReader {
protected HashMap sections = new LinkedHashMap();
private transient String currentSecion;
private transient Properties current;
private final String filePath=ConfigUtil.getInstance().getValue("config_ini_filepath");
public static IniReader iniFile=new IniReader();
public static IniReader getInstance(){
return iniFile;
}
private IniReader() {}
private void read() throws IOException {
// modifyed by brmrk
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
reader.close();
}
/**
* 描述:处理读取ini文件的每一行
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:53:36
*/
protected void parseLine(String line) {
line = line.trim();
if (line.matches("//[.*//]")) {
currentSecion = line.replaceFirst("//[(.*)//]", "$1");
current = new Properties();
} else if (line.matches(".*=.*")) {
int i = line.indexOf('=');
String name = line.substring(0, i);
String value = line.substring(i + 1);
current.setProperty(name.trim(), value.trim());
sections.put(currentSecion, current);
}
}
/**
* 描述:根据传入的片段名称和值的名字获取对应的数值
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:09
*/
public String getValue(String section, String name) throws IOException {
System.out.println("section="+section+"name="+name);
this.read();
Properties p = (Properties) sections.get(section);
if (p == null) {
return null;
}
String value = p.getProperty(name);
System.out.println("-------------------------get value="+value);
return value;
}
/**
*
* 描述:根据片段名字,值的名字,值的数值进行更新
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:54:40
*/
public boolean changeProperty(String sectionName,String key,String value) throws IOException,IllegalArgumentException{
if(!StringUtils.isEmpty(key)){
return false;
}else{
this.read();//重新读取文件到内存中
Properties p=(Properties)sections.get(sectionName);
if(null == p){
throw new IllegalArgumentException("sectionName参数出错");
}
p.setProperty(key, value);
this.writeProperty();
return true;
}
}
/**
*
* 描述:将修改后的值写入配置文件
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:22
*/
private void writeProperty() throws IOException{
PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
try {
StringBuilder builder=new StringBuilder();
Iterator<String> iterator=sections.keySet().iterator();
while(iterator.hasNext()){
String sectionName=iterator.next();
Properties p=(Properties)sections.get(sectionName);
builder.append("/n").append("/n").append("[").append(sectionName).append("]");
builder.append(getPropertiesString(p));
}
writer.write(builder.toString().trim());
writer.flush();
writer.close();
} finally{
if(writer != null){
writer.close();
}
}
}
/**
*
* 描述:获取组织好的properties文件的字符串表示
* @author: erguai123@yahoo.com.cn
* @createtime: 2010-5-11 下午02:55:49
*/
private String getPropertiesString(Properties p){
StringBuilder builder=new StringBuilder();
Iterator iterator=p.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next().toString();
builder.append("/n").append(key).append("=").append(p.get(key));
}
return builder.toString();
}
}
相关文章推荐
- JAVA读取ini文件(很简单不带section)
- [Java] Java序列化将一个对象的内容保存到文件和从文件读取对象
- 稀奇稀奇真稀奇--使用java Map读取neo4j数据碰到的一个困惑
- 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
- JAVA多线程读取同一个文件,加速对文件内容的获取
- java读取、写入保存、遍历ini文件配置数据
- Java中读取一个TXT文件中的每行内容的前5个字符,并保存到另一个TXT文件中。
- 一个JAVA读取.properties文件的例子(lp)
- Java 多线程读取一个文件
- 一个用java读取XML文件的简单方法(转)
- java读取一个文件下字符串的个数
- (vc)分享一个读写ini文件的类,支持多种数据类型的读写,二进制数据都能保存和读取
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- 读取XML文件里其中一个结点的值(Java)
- [Source Code]Java I/O --- ---使用dom读取一个xml文件的叶子节点的text content
- 一个下午,找了一条小虫,JAVA读取fits文件出错异常
- java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- java读取输入流并保存成一个文件
- Java读取一个文本文件拼接成一个字符串(readFileToString)
- 如何用java读取一个网站的源文件 保存到本地