?? taskproperties.java
字號(hào):
/*
* *****************************************************
* 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;
/**
* 自定義的對(duì)配置文件進(jìn)行操作類。
* @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 );
}
/**
* 打開(kāi)指定配置文件,并把參數(shù)和對(duì)應(yīng)的屬性放置到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();
}
}
/**
* 判斷是否包含指定的參數(shù)。
* @param key 參數(shù)名。
* @return
*/
public boolean isContainKey( String key )
{
return itsMap.containsKey( key );
}
/**
* 根據(jù)指定的參數(shù)獲得其屬性。
* @param key 參數(shù)名
* @return
*/
public String getProperty(String key)
{
if (!itsMap.containsKey(key))
return null;
return (String) itsMap.get(key);
}
/**
* 把指定的參數(shù)和其屬性寫入到配置文件中。
* 其思想是,首先把已經(jīng)存在的配置文件的內(nèi)容拷貝到一個(gè)臨時(shí)文件中,
* 最后把新設(shè)置的參數(shù)和屬性寫入到臨時(shí)文件中。
* 然后把已經(jīng)存在的配置文件刪除,把臨時(shí)文件命名為已經(jīng)存在的配置文件。
* @param key 參數(shù)名
* @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);
}
}
/**
* 改變指定參數(shù)的值。
* 其思想setProperty方法。
* @param key 參數(shù)名
* @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);
}
}
/**
* 刪除指定參數(shù)的屬性。
* 其思想同setProperty方法。
* @param key 參數(shù)名
*/
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);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -