📄 taskproperties.java
字号:
/*
* *****************************************************
* Copyright (c) 2005 IIM Lab. All Rights Reserved.
* Created by xuehao at 2005-10-12
* Contact: zxuehao@mail.ustc.edu.cn
* *****************************************************
*/
package org.indigo.util;
import java.io.*;
import java.util.HashMap;
/**
* 自定义的对配置文件进行操作类。
* @author wbz
*
*/
public class TaskProperties
{
private HashMap itsMap = new HashMap();
private File itsFile = null;
public TaskProperties()
{
}
public TaskProperties(String file)
{
// if (itsFile != null)
itsFile = new File(file);
// System.out.println( file );
}
/**
* 打开指定配置文件,并把参数和对应的属性放置到HashMap中。
* @param file
*/
public void open(String file)
{
BufferedReader rd = null;
try
{
rd = new BufferedReader(new InputStreamReader(new FileInputStream(
file), "gb2312"));
itsFile = new File(file);
} catch (UnsupportedEncodingException e)
{
System.out.println( "File error: " + file );
e.printStackTrace();
} catch (FileNotFoundException e)
{
System.out.println( "File error: " + file );
e.printStackTrace();
}
String key, val, str = null;
do
{
try
{
str = rd.readLine();
} catch (IOException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (str == null)
break;
str = str.trim();
if (str.startsWith("#"))
continue;
int i = 0;
i = str.indexOf("=");
if (i == -1)
continue;
key = str.substring(0, i);
val = str.substring(i + 1);
if (!itsMap.containsKey(key))
itsMap.put(key, val);
} while (str != null);
try
{
rd.close();
} catch (IOException e1)
{
e1.printStackTrace();
}
}
/**
* 判断是否包含指定的参数。
* @param key 参数名。
* @return
*/
public boolean isContainKey( String key )
{
return itsMap.containsKey( key );
}
/**
* 根据指定的参数获得其属性。
* @param key 参数名
* @return
*/
public String getProperty(String key)
{
if (!itsMap.containsKey(key))
return null;
return (String) itsMap.get(key);
}
/**
* 把指定的参数和其属性写入到配置文件中。
* 其思想是,首先把已经存在的配置文件的内容拷贝到一个临时文件中,
* 最后把新设置的参数和属性写入到临时文件中。
* 然后把已经存在的配置文件删除,把临时文件命名为已经存在的配置文件。
* @param key 参数名
* @param value 属性值
*/
public synchronized void setProperty(String key, String value)
{
boolean error = false;
File tempFile = null;
String str = null;
BufferedReader in = null;
BufferedWriter out = null;
key = key.trim();
try
{
in = new BufferedReader(new InputStreamReader(new FileInputStream(
itsFile)));
tempFile = new File(itsFile.getParentFile(), itsFile.getName()
+ ".tmp");
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(tempFile)));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
do
{
try
{
str = in.readLine();
} catch (IOException e2)
{
e2.printStackTrace();
}
if (str == null)
break;
try
{
String keyStr = null;
int i;
i = str.indexOf("=");
if (i != -1)
keyStr = str.substring(0, i);
if (keyStr != null && keyStr.equals(key))
continue;
out.write(str + "\n");
} catch (IOException e1)
{
error = true;
e1.printStackTrace();
}
} while (str != null);
str = key + "=" + value;
try
{
out.write(str + "\n");
} catch (IOException e1)
{
error = true;
e1.printStackTrace();
} finally
{
try
{
in.close();
out.close();
} catch (IOException e2)
{
error = true;
e2.printStackTrace();
}
}
if (!error)
{
// System.out.println( "no error!" );
itsFile.delete();
tempFile.renameTo(itsFile);
}
}
/**
* 改变指定参数的值。
* 其思想setProperty方法。
* @param key 参数名
* @param value 属性值
*/
public synchronized void changeProperty(String key, String value)
{
boolean error = false;
File tempFile = null;
String str = null;
BufferedReader in = null;
BufferedWriter out = null;
key = key.trim();
try
{
// System.out.println( itsFile );
in = new BufferedReader(new InputStreamReader(new FileInputStream(
itsFile)));
tempFile = new File(itsFile.getParentFile(), itsFile.getName()
+ ".tmp");
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(tempFile)));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
do
{
try
{
str = in.readLine();
} catch (IOException e2)
{
e2.printStackTrace();
}
if (str == null)
break;
try
{
String keyStr = null;
int i;
i = str.indexOf("=");
if (i != -1)
keyStr = str.substring(0, i);
if (keyStr != null && keyStr.equals(key))
str = keyStr + "=" + value;
out.write(str + "\n");
} catch (IOException e1)
{
error = true;
e1.printStackTrace();
}
} while (str != null);
try
{
in.close();
out.close();
} catch (IOException e2)
{
error = true;
e2.printStackTrace();
}
if (!error)
{
itsFile.delete();
tempFile.renameTo(itsFile);
}
}
/**
* 删除指定参数的属性。
* 其思想同setProperty方法。
* @param key 参数名
*/
public synchronized void deleteProperties(String key)
{
boolean error = false;
File tempFile = null;
String str = null;
BufferedReader in = null;
BufferedWriter out = null;
key = key.trim();
try
{
in = new BufferedReader(new InputStreamReader(new FileInputStream(
itsFile)));
tempFile = new File(itsFile.getParentFile(), itsFile.getName()
+ ".tmp");
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(tempFile)));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
do
{
try
{
str = in.readLine();
} catch (IOException e2)
{
e2.printStackTrace();
}
if (str == null)
break;
try
{
String keyStr = null;
int i;
i = str.indexOf("=");
if (i != -1)
keyStr = str.substring(0, i);
if (keyStr != null && keyStr.equals(key))
continue;
out.write(str + "\n");
} catch (IOException e1)
{
error = true;
e1.printStackTrace();
}
} while (str != null);
try
{
in.close();
out.close();
} catch (IOException e2)
{
error = true;
e2.printStackTrace();
}
if (!error)
{
itsFile.delete();
tempFile.renameTo(itsFile);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -