类配置文件操作,操作没有段的ini文件

//自己写的一个简单类用于操作配置文件

//应该已经有类似的东西了

//(⊙o⊙)…,有bug请发我邮箱suweizhao3128@163.com

import java.io.File;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;

public class ConfigureFile
{

 public LinkedList<ConfigureRecord> records = new LinkedList<ConfigureRecord>();
 /**
  * 默认的构造函数,根据指定的文件名读入记录
  * @param filename 文件名
  */
 public ConfigureFile(String filename)
 {
  Scanner input  = null;
  try
  {
   File file = new File(filename);
   if(! file.exists() )
    return ;
   input = new Scanner(file);
   while(input.hasNext())
   {
    String str = input.nextLine();
    String[]str2 = str.split("=",2);
    ConfigureRecord newRecord = new ConfigureRecord();
    newRecord.setKey(str2[0]);
    newRecord.setValue(str2[1]);
    records.add(newRecord);
   }
   
  }
  catch(Exception ex)
  {
   System.out.printf(ex.getMessage());
  }
  finally
  {
   if(input != null)
    input.close();
  }
 }
 /**
  * 根据给定的key返回对应的value
  * @param key
  * @return 返回满足key的第一个value
  */
 public String getValue(String key,String defaultValue)
 {
  if(records.isEmpty())
   return defaultValue ;
  for(int i = 0 ; i < records.size() ; i ++ )
  {
   ConfigureRecord temp = records.get(i);
   if( temp.getKey().equals(key) )
    return temp.getValue() ;
  }
  return defaultValue ;
 }
 /**
  * 根据给定的key返回对应的value
  * @param key 关键词
  * @return 返回满足key的所有value
  */
 public String[] getValues(String key,String defaultValue)
 {
  if(records.isEmpty())
   if( defaultValue != null )
    return new String[]{ defaultValue };
   else
    return null ;
  int count  = 0 ;
  for(int i = 0 ; i < records.size() ; i ++ )
  {
   ConfigureRecord temp = records.get(i);
   if( temp.getKey().equals(key) )
   {
    count ++ ;
   }
  }
  String[]strs = new String[count] ;
  count = 0 ;
  for(int i = 0 ; i < records.size() ; i ++ )
  {
   ConfigureRecord temp = records.get(i);
   if( temp.getKey().equals(key) )
   {
    strs[count++] = temp.getValue() ;
   }
  }
  return strs ;
 }
 /**
  * 写入指定的key及value的记录,并指定是否执行重复检测
  * @param key 关键词
  * @param value 值
  * @param test 指定是否执行存在检测
  * @return 返回true说明添加成功,false代表失败
  */
 public boolean Add(String key,String value,boolean test)
 {
  if(key.indexOf("=") > 0 )
   return false ;
  if(test)
  {
   for(int i = 0 ; i < records.size() ; i ++ )
   {
    ConfigureRecord temp = records.get(i);
    if( temp.getKey().equals(key) )
    {
     temp.setValue(value) ;
     return false ;
    }
   }
   
  }
  ConfigureRecord newRecord = new ConfigureRecord();
  newRecord.setKey(key);
  newRecord.setValue(value);
  records.add(newRecord);
  return true ;
 }
 /**
  * 删除指定key的记录,
  * 指定all标志为true则将删除全部满足key的记录
  * @param key 关键词
  * @param all 指定是否删除所有满足key的记录
  */
 public void Remove(String key,boolean all)
 {
  for(int i = 0 ; i < records.size() ; i ++ )
  {
   ConfigureRecord temp = records.get(i);
   if( temp.getKey().equals(key) )
   {
    records.remove(i) ;
    i -- ;
    if(! all)
     return ;
   }
  }
 }
 /**
  * 保存记录中的数据到文件中,并指定是否覆盖,同时清空列表
  * @param filename 保存的文件名(详细路径)
  * @param override 指定是否覆盖
  */
 public void Save(String filename,boolean override)
 {
  LinkedList<String> strs = new LinkedList<String>();
  while(true)
  {
   if(! override)
   {
    Scanner input  = null;
    try
    {
     File file = new File(filename);
     if(! file.exists() )
      break ;
     input = new Scanner(file);
     while(input.hasNext())
     {
      String str = input.nextLine();
      strs.add(str);
     }
     break;
    
    }
    catch(Exception ex)
    {
     System.out.printf("文件不存在或打开失败!");
     break ;
    }
    finally
    {
     if(input != null)
      input.close();
    }
   }
   break ;
  }
  if(!strs.isEmpty() || !records.isEmpty())
  {
   try
   {
    PrintWriter writer  = new PrintWriter( filename);
    while(!strs.isEmpty())
    {
     writer.println(strs.removeFirst());
    }
    while(!records.isEmpty())
    {
     ConfigureRecord record = records.removeFirst();
     writer.println( record.getKey()+"="+record.getValue());
    }
    writer.close();
   }
   catch (Exception e)
   {
    System.out.printf("文件不存在或打开失败!");
   }
  }
 }
 public static void main(String[]ps)
 {
  /*ConfigureFile file = new ConfigureFile("sdsd.ini");
  file.Add("我", "kkk1", false);
  file.Add("我", "kkk2", false);
  file.Remove("我", false);
  file.Add("我", "kkk3", false);
  file.Add("我", "kkk4", false);
  String str = file.getValue("我", null);
  if(str != null )
   System.out.println(str);
  String[] strs = file.getValues("我", null);
  if(strs != null && strs.length > 0)
  {
   for(int i = 0 ; i < strs.length ; i ++)
    System.out.println(strs[i]);
  }
  file.Add("我", "kkk5", false);
  file.Add("我", "kkk6", true);
  file.Save("sdsd.ini", false);*/
 }
}
class ConfigureRecord
{
 private String key ="";
 private String value= "" ;
 public String getKey() {
  return key;
 }
 public void setKey(String key) {
  this.key = key;
 }
 public String getValue() {
  return value;
 }
 public void setValue(String value) {
  this.value = value;
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值