INI参数(文本文件)的读写

2016-02-19 16:26 1 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的INI参数(文本文件)的读写,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - 编程语言 】

//参数(文本文件)的读写,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 + ";n";
isHere = true;
}else{
LineStr = PName + "=" + PValue + ";n";
}
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 + ";n";
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 + "n";
}
}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);
}
}

来源:https://www.tulaoshi.com/n/20160219/1612119.html

延伸阅读
标签: 电脑入门
Linux系统中如果你想要搜索文本文件的话,可以使用grep命令,通过grep命令,你可以搜索相关关键词文件,也可对符合条件的文本文件进行筛选,是个比较常用的命令,下面图老师小编就给大家介绍下Linux使用grep命令搜索文本文件的方法。 grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后...
标签: Web开发
一、读取文本文件的步骤如下: 1、创建 FileSystemObject 对象实例; % Set fso=Server.CreateObject("Scripting.FileSystemObject") % 2、使用FileSystemObject对象的OpenTextFile方法返回一个 TextStream 对象实例; % Set txtFile=fso.OpenTextFile(filename[, iomode[, create[, format]]]) % ...
MySQL写入数据通常用insert语句,如 代码如下: insert into person values(张三,20),(李四,21),(王五,70)…; 但有时为了更快速地插入大批量数据或交换数据,需要从文本中导入数据或导出数据到文本。 一、 建立测试表,准备数据 首先建立一个用于测试的表示学生信息的表,字段有id、姓名、年龄、城市、薪水。Id和姓名不 能...
标签: ASP
  Ever want to know how to display the contents of a text document using ASP. Here is a easy way to read from a text file    <!--Start of ASP Code---- <% 'by James Seymour, http://jamesdot.org Dim write Dim fileSysObj, tf, read ' Read the read.txt ' Store the file name where the Inform...
Oracle数据直接导出到文本文件的方法 利用Oracle中的Spool缓冲池技术可以实现Oracle数据导出到文本文件。 1)、在Oracle PL/SQL中输入缓冲开始命令,并指定输出的文件名: spool d:output.txt 2)、在命令行中随便输入你的SQL查询: select mobile from customer; select mobile from client; …… 3)、在命令行中输入缓冲结果命令: spool

经验教程

935

收藏

53
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部