<%-- JSP全站自动生成HTML下载新闻系统 V3.1 作者:JSP天空网站长 曹进 QQ:173241393 --%> INI参数(文本文件)的读写,by chillming
 
用户名:
密码:
终身会员
  首页 网络文摘 技术专题 下载中心 JSP培训 公司产品 JSP虚拟主机 技术论坛 会员 JSP免费空间  
  技术文摘: JSP基础 | JSP高级 | JSP环境 | JAVA基础 | JAVA高级 | JAVA工具 | JAVA数据库 | WEB框架 | XML相关 | JAVA考试 | J2EE相关 | J2ME相关 |
  视频下载: 程序设计数据库相关教程计算机基础教程图形图像网络技术网络安全时空书库EasyShop虚拟主机JSP培训技术论坛
  书籍教程: JSP教程Java教程图像软件XML教程数据库教程网页教程工具软件服务器相关ASP教程PHP教程CGI教程.NET教程
  代码下载: ASP代码下载JSP代码下载PHP代码下载CGI代码下载Flash代码C/C++/VCPowerBuilderDelphiVisualBasicVisualFoxpro
您的位置: 首页 -> 文章分类 -> JSP高级 -> INI参数(文本文件)的读写,by chillming
 文摘搜索
 
 文摘资源分类
 
 文摘总排行榜TOP10
 
 最新更新文摘TOP10
 

INI参数(文本文件)的读写,by chillming

[来源]www.jspsky.com管理员   [作者]网络文摘   [时间]2005-4-7
推荐等级: 点击:
//参数(文本文件)的读写,by chillming

import java.io.*;

public class IniFile{
private String FileName = "";

public IniFile(String fn){
FileName = fn;
}

//返回参数的值
public String ReadPara(String Para,String PV){
String PName = "";
String PValue = PV;
String LineStr = "";
int pos = 0;

try{
BufferedReader RFile = new BufferedReader(new FileReader(FileName));
while(true){
try{
LineStr = RFile.readLine();
pos = LineStr.indexOf("=");
PName = LineStr.substring(0,pos).trim();
if(PName.equals(Para.trim())){
//找到
PValue = LineStr.substring(pos+1,LineStr.indexOf(";")).trim();
break;
}
}catch(NullPointerException e){
//文件尾
break;
}
}
RFile.close();
}catch (IOException e)
{
if(PV==null)PV = "";
return PV;
}

return PValue;
}


//更新或添加参数
public boolean WritePara(String PN,String PV){
String PName = "";
String PValue = "";
String LineStr = "";
String AllLines = "";
int pos = 0;
boolean isHere = false;
if(PV==null)PV = "";
try{
BufferedReader RFile = null;

try{
RFile = new BufferedReader(new FileReader(FileName));
}
catch(FileNotFoundException e){
//文件不存在
File file = new File(FileName);
file.createNewFile();
RFile = new BufferedReader(new FileReader(FileName));
}

while(true){
try{
LineStr = RFile.readLine();

pos = LineStr.indexOf("=");
PName = LineStr.substring(0,pos).trim();
PValue = LineStr.substring(pos+1,LineStr.indexOf(";")).trim();

if(PName.equals(PN)){
LineStr = PName + "=" + PV + "; ";
isHere = true;
}else{
LineStr = PName + "=" + PValue + "; ";
}

if(LineStr!=null && LineStr !=""){
AllLines += LineStr;
}
}catch(NullPointerException e){
//文件尾
break;
}
}

RFile.close();
}catch (IOException e) {
return false;
}

try{
BufferedWriter Fout=new BufferedWriter(new FileWriter(FileName));
if(!isHere){
LineStr = PN + "=" + PV + "; ";
AllLines += LineStr;
}
Fout.write(AllLines);
Fout.close();
}catch (IOException e)
{
System.out.println("io error write");
return false;
}

return true;
}

//删除参数
public boolean DelPara(String PN){
String PName = "";
String LineStr = "";
String AllLines = "";
int pos = 0;

try{
BufferedReader RFile = new BufferedReader(new FileReader(FileName));

while(true){
try{
LineStr = RFile.readLine();

pos = LineStr.indexOf("=");
PName = LineStr.substring(0,pos).trim();

if(!PName.equals(PN)){
AllLines += LineStr + " ";
}
}catch(NullPointerException e){
//文件尾
break;
}
}

RFile.close();
}catch (IOException e) {
return false;
}

try{
BufferedWriter Fout=new BufferedWriter(new FileWriter(FileName));
Fout.write(AllLines);
Fout.close();
}catch (IOException e)
{
return false;
}

return true;
}

public static void main(String args[]) {
String fn = "Config.txt";
String Pv = "";

//System.out.println(System.getProperty("user.dir"));

IniFile inifile = new IniFile(fn);

inifile.WritePara("java","chillming");
inifile.WritePara("java1","chillming1");
inifile.WritePara("java2","chillming2");

inifile.DelPara("java1");

Pv = inifile.ReadPara("java2","java2");
System.out.println(Pv);
Pv = inifile.ReadPara("java1","java1");
System.out.println(Pv);
Pv = inifile.ReadPara("java","java0");
System.out.println(Pv);
}
}
[收藏][打印][关闭]
在线咨询服务 在线咨询服务 在线咨询服务 在线咨询服务
Copyright©2005-2006陕西思远数码科技有限责任公司 服务电话:029-88212987 传真:029-88278265 售后服务QQ:173241393
地址:陕西省西安市吉祥路186号太白新苑C座2104 陕ICP备05004508号