解决方案 »
- 在eclipse中打包程序为jar文件,放到linux上执行不成功
- 如何调用JFrame的windowClosing方法
- 一个用生产者消费者线程进行文件拷贝的问题!想不通了~大家帮忙!
- 关于socket 即时响应
- DefaultMutableTreeNode如何使用
- java 高手帮帮我
- 文件的Cpoy
- 新学java一道关于算法的题,请给位高手帮忙。
- Java读取控制 error code=109 message=管道已结束
- [高分]一直都在研究JTree做树型结构,可是怎么都弄不是很清楚,哪位有比较详细的例子呀?
- 关系Float的除法。好像不妥。
- 在Jbuilder中如何根据条件从表格中取的唯一的记录?
import java.util.*;public class IniFileHandler
{
private LinkedList lstContentLine;
public IniFileHandler()
{
init();
}
public IniFileHandler(String filepath,boolean bcreate)
{
sFilePath = filepath;
init();
}
private void init()
{
//used in the constructor
lstContentLine = new LinkedList();
}
public void create() throws Exception
{
//read the ini file into this object
File f = new File(getFilePath());
FileInputStream fis = new FileInputStream(f);
DataInputStream di = new DataInputStream(fis);
String line = di.readLine();
while(line!=null)
{
line = trimLine(line);
if(line!=null)
lstContentLine.add(line);
line = di.readLine();
}
di.close();
}
private String trimLine(String s)
{
//remove leading spaces of the line
//remove spaces before "="
if(s==null)
return s;
String s_raw = s;
s = s.trim();
if(s==null)
return s_raw;
int npos=s.indexOf("=");
if(npos<=0)
return s;
String sleft = s.substring(0,npos); //showDebug("56,"+sleft);
int nleftlen = sleft.length()-1;
int spacepos= nleftlen;
while(sleft.charAt(spacepos)==' ')
spacepos--;
if(spacepos ==nleftlen)//no space before "=",return the original line
return s;
String sright = s.substring(npos);
return sleft.substring(0,spacepos+1)+sright;
}
private String getFilePath()
{
return sFilePath;
}
private String sFilePath;
public void save() throws Exception
{
//save the ini
saveAs(sFilePath);
}
public void saveAs(String saveaspath) throws Exception
{
if(saveaspath==null)
{
showError("22,path is null in saveAs()");
return ;
}
File fsave=new File(saveaspath);
if(fsave.exists()==false)
fsave.createNewFile();
FileOutputStream fos = new FileOutputStream(fsave);
StringBuffer sbtmp=new StringBuffer();
int line_num = lstContentLine.size();
String line_separator = System.getProperty("line.separator");
for(int i=0;i<line_num;i++)
{
String line = (String)lstContentLine.get(i);
sbtmp.append(line);
sbtmp.append(line_separator);
}
fos.write(sbtmp.toString().getBytes());
fos.flush();
fos.close();
}
public String getValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
return cursect.getValue(valuename,lstContentLine);
}
public void setValue(String sectionname,String valuename, String value)
{
//if the section does not exist, create it.
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
cursect = createSection(sectionname);
} //showDebug("116,"+cursect.toString());
cursect.setValue(valuename,value,lstContentLine);
}
public void deleteValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
showDebug("125,"+sectionname+" does not exist");
}
cursect.deleteValue(valuename,lstContentLine); //showDebug("126,"+sectionname+" deleted");
}
private Section createSection(String sectionname)
{
String section_name = formatSectionName(sectionname);
lstContentLine.addLast(section_name);
int lines = lstContentLine.size()-1;
Section sectionobj = new Section();
sectionobj.setName(section_name);
sectionobj.setPos(lines,lines);
return sectionobj;
}
public void deleteSection(String sectionname)
{
Section sectionobj = findSection(sectionname);
if(sectionobj.isExisted()==false)
{
showDebug("145,deleteSection("+sectionobj.getName()+") does not exist");
return;
}
sectionobj.delete(lstContentLine);
}
private String formatSectionName(String sectionname)
{
//return "[section_name]"
String section_name;
if(sectionname.startsWith("["))
section_name = sectionname;
else
section_name = "["+sectionname+"]";
return section_name;
}
private Section findSection(String secnameraw)
{
String secname = formatSectionName(secnameraw);
Section sectionobj = new Section();
int find_line_pos=-1;
int end_line_pos = -1;
int line_num = lstContentLine.size();
for(int i=0;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.compareToIgnoreCase(secname)==0)
{
find_line_pos = i;
break;
}
}
//find section end
if(find_line_pos>=0)
end_line_pos = findLine("[",find_line_pos+1);
if(end_line_pos<0)
end_line_pos = lstContentLine.size()-1;
if(end_line_pos<0)
end_line_pos = find_line_pos;
sectionobj.setPos(find_line_pos,end_line_pos);
sectionobj.setName(secname);
return sectionobj;
}
private int findLine(String startline, int startfrom)
{
//find the first line starts with "startline" passed in
int find_line_pos=-1;
int line_num = lstContentLine.size();
if(startfrom >=line_num)
return -1;
for(int i=startfrom;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.startsWith(startline))
{
find_line_pos = i;
break;
}
}
return find_line_pos;
}
private static void showError(String s)
{
System.out.println("IniFileHandler:"+s);
}
private static void showDebug(String s)
{
System.out.println("IniFileHandler:"+s);
}
import java.util.*;public class IniFileHandler
{
private LinkedList lstContentLine;
public IniFileHandler()
{
init();
}
public IniFileHandler(String filepath,boolean bcreate)
{
sFilePath = filepath;
init();
}
private void init()
{
//used in the constructor
lstContentLine = new LinkedList();
}
public void create() throws Exception
{
//read the ini file into this object
File f = new File(getFilePath());
FileInputStream fis = new FileInputStream(f);
DataInputStream di = new DataInputStream(fis);
String line = di.readLine();
while(line!=null)
{
line = trimLine(line);
if(line!=null)
lstContentLine.add(line);
line = di.readLine();
}
di.close();
}
private String trimLine(String s)
{
//remove leading spaces of the line
//remove spaces before "="
if(s==null)
return s;
String s_raw = s;
s = s.trim();
if(s==null)
return s_raw;
int npos=s.indexOf("=");
if(npos<=0)
return s;
String sleft = s.substring(0,npos); //showDebug("56,"+sleft);
int nleftlen = sleft.length()-1;
int spacepos= nleftlen;
while(sleft.charAt(spacepos)==' ')
spacepos--;
if(spacepos ==nleftlen)//no space before "=",return the original line
return s;
String sright = s.substring(npos);
return sleft.substring(0,spacepos+1)+sright;
}
private String getFilePath()
{
return sFilePath;
}
private String sFilePath;
public void save() throws Exception
{
//save the ini
saveAs(sFilePath);
}
public void saveAs(String saveaspath) throws Exception
{
if(saveaspath==null)
{
showError("22,path is null in saveAs()");
return ;
}
File fsave=new File(saveaspath);
if(fsave.exists()==false)
fsave.createNewFile();
FileOutputStream fos = new FileOutputStream(fsave);
StringBuffer sbtmp=new StringBuffer();
int line_num = lstContentLine.size();
String line_separator = System.getProperty("line.separator");
for(int i=0;i<line_num;i++)
{
String line = (String)lstContentLine.get(i);
sbtmp.append(line);
sbtmp.append(line_separator);
}
fos.write(sbtmp.toString().getBytes());
fos.flush();
fos.close();
}
public String getValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
return cursect.getValue(valuename,lstContentLine);
}
public void setValue(String sectionname,String valuename, String value)
{
//if the section does not exist, create it.
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
cursect = createSection(sectionname);
} //showDebug("116,"+cursect.toString());
cursect.setValue(valuename,value,lstContentLine);
}
public void deleteValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
showDebug("125,"+sectionname+" does not exist");
}
cursect.deleteValue(valuename,lstContentLine); //showDebug("126,"+sectionname+" deleted");
}
private Section createSection(String sectionname)
{
String section_name = formatSectionName(sectionname);
lstContentLine.addLast(section_name);
int lines = lstContentLine.size()-1;
Section sectionobj = new Section();
sectionobj.setName(section_name);
sectionobj.setPos(lines,lines);
return sectionobj;
}
public void deleteSection(String sectionname)
{
Section sectionobj = findSection(sectionname);
if(sectionobj.isExisted()==false)
{
showDebug("145,deleteSection("+sectionobj.getName()+") does not exist");
return;
}
sectionobj.delete(lstContentLine);
}
private String formatSectionName(String sectionname)
{
//return "[section_name]"
String section_name;
if(sectionname.startsWith("["))
section_name = sectionname;
else
section_name = "["+sectionname+"]";
return section_name;
}
private Section findSection(String secnameraw)
{
String secname = formatSectionName(secnameraw);
Section sectionobj = new Section();
int find_line_pos=-1;
int end_line_pos = -1;
int line_num = lstContentLine.size();
for(int i=0;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.compareToIgnoreCase(secname)==0)
{
find_line_pos = i;
break;
}
}
//find section end
if(find_line_pos>=0)
end_line_pos = findLine("[",find_line_pos+1);
if(end_line_pos<0)
end_line_pos = lstContentLine.size()-1;
if(end_line_pos<0)
end_line_pos = find_line_pos;
sectionobj.setPos(find_line_pos,end_line_pos);
sectionobj.setName(secname);
return sectionobj;
}
private int findLine(String startline, int startfrom)
{
//find the first line starts with "startline" passed in
int find_line_pos=-1;
int line_num = lstContentLine.size();
if(startfrom >=line_num)
return -1;
for(int i=startfrom;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.startsWith(startline))
{
find_line_pos = i;
break;
}
}
return find_line_pos;
}
private static void showError(String s)
{
System.out.println("IniFileHandler:"+s);
}
private static void showDebug(String s)
{
System.out.println("IniFileHandler:"+s);
}
{
//open myconfig.ini
try
{
IniFileHandler myconfig=new IniFileHandler("myconfig1.ini",false);
myconfig.create();
String svalue=myconfig.getValue("[mysection1]","API:PORT");
showDebug("[mysection1] API:PORT="+svalue);
svalue = "testofapiport";
myconfig.setValue("[mysection1]","API:PORT",svalue);
myconfig.setValue("[mysection1]","API:PORT2",svalue);
myconfig.setValue("[mysection1]","API:PORT3",svalue);
myconfig.setValue("[mysection1]","API:PORT4",svalue);
myconfig.setValue("[mysection1]","API:PORT5",svalue); myconfig.save();
showDebug("program end");
}
catch(Exception e)
{
//showError(e.toString());
e.printStackTrace();
}
}
}class Section extends Object
{
public Section()
{
init();
}
public Section(String s)
{
setName(s);
init();
}
private void init()
{
setPos(-1,-1);
setName("");
}
private String sName;
public String getName()
{
return sName;
}
public void setName(String s)
{
if(s==null)
sName = "";
else
sName = s;
}
private int nStartLine;
private int nEndLine;
public void setPos(int start,int nend)
{
nStartLine = start;
nEndLine = nend;
}
public boolean isExisted()
{
return nStartLine!=-1;
}
public void setValue(String valuename,String value,LinkedList lst)
{
int nbegin = nStartLine;
int nend = nEndLine;
if(nend<nbegin)
nend = nbegin;
String value_prefix = valuename+"=";
String newline = valuename+"=" + value;
for(int i=nbegin;i<=nend;i++)
{
String line = (String)lst.get(i);
if(line.startsWith(value_prefix))
{
//replace the value
lst.set(i,newline);
return;
}
}
//the value does not exist, insert it to the section end
lst.add(nend,newline);
nend++;
}
public String getValue(String valuename,LinkedList lst)
{
if(isExisted())
{
int nbegin = nStartLine;
int nend = nEndLine;
if(nend<=nbegin)
return "";
String value_prefix = valuename+"=";
for(int i=nbegin;i<=nend;i++)
{
String line = (String)lst.get(i);
if(line.startsWith(value_prefix))
{
return line.substring(value_prefix.length());
}
}
}
return "";
}
public void deleteValue(String valuename,LinkedList lst)
{
if(isExisted()==false)
{
showDebug("314,section does not exist");
return;
}
int nbegin = nStartLine;
int nend = nEndLine;
if(nend<=nbegin)
{
showDebug("321,section is void");
return;
}
String value_prefix = valuename+"=";
for(int i=nbegin;i<=nend;i++)
{
String line = (String)lst.get(i);
if(line.startsWith(value_prefix))
{
lst.remove(i);
showDebug("331,delete value at line:"+i);
return;
}
}
showDebug("335,"+valuename +" is not found in section "+getName());
}
public void delete(LinkedList lst)
{
if(isExisted()==false)
{
showDebug("363,deleteSection("+getName()+") does not exist");
return;
}
for(int i=nEndLine;i>=nStartLine;i--)
lst.remove(i);
}
private static void showError(String s)
{
System.out.println("Section:"+s);
}
private static void showDebug(String s)
{
System.out.println("Section:"+s);
}
public String toString()
{
return "startline:"+nStartLine+",end line:"+nEndLine;
}
}
thank you masterZ!
can you tell me what's the meaning of "gz"?
如果是XML的,Sax和Dom就可以。还挺方便。
http://www.csdn.net/Develop/read_article.asp?id=13885
// public void loadConfig() throws Exception
{
//配置
FileInputStream file=new FileInputStream(fileName);
pro = new Properties();
pro.load(file);
//取其中的参数
String p_host,p_port,p_timeout,p_log;
p_host=pro.getProperty("host").trim();
p_port=pro.getProperty("port");
p_timeout=pro.getProperty("timeout");
......
}//myfile.property的参数配置形式
host=130.130.10.18
port=5080
#port=1008
timeout=100000
FileInputStream file=new FileInputStream(fileName); 这个fileName本来是以参数传进来的,这里应该是FileInputStream file=new FileInputStream("myfile.property");