📄 propertyloader.java
字号:
package com.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
/**
* description: 根据提供的路径,将属性文件装载到内存中
*
* Created on 2005-7-25
* @author WuQiaoYun
*
*/
public class PropertyLoader {
private static Hashtable pptContainer = new Hashtable();
private static String directory = null;
/**
* 通过继承接口FilenameFilter,过滤文件名
*
*/
private static class PptFilter implements FilenameFilter {
public boolean accept(File dir, String name)
{
return name.endsWith(extention);
}
private String extention;
public PptFilter(String name)
{
extention = "." + name;
}
}
public PropertyLoader(){
}
/**
* 将目录下所有的扩展名是".properties"的文件装载到此类中
* @param pathname property文件所在目录的绝对路径
* @return 装载成功,返回true; 反之,返回false
*/
public static boolean load(String pathname)
{
File dir = new File(pathname);
if(!dir.isDirectory()){
System.out.println("PropertyLoader: (load)Not directory");
return false;
}
String names[] = dir.list(new PptFilter("properties"));
if(names == null){
System.out.println("PropertyLoader: (load)Haven't properties files");
return false;
}
int i = 0;
directory = dir.getAbsolutePath();
String pptname;
while (i<names.length) {
pptname = names[i];
int idx = pptname.indexOf(".properties");
pptname = pptname.substring(0, idx);
FileInputStream fis = null;
Properties ppt = null;
try
{
fis = new FileInputStream(directory + File.separatorChar + names[i]);
ppt = new Properties();
ppt.load(fis);
fis.close();
}
catch(FileNotFoundException e){
try{
fis.close();
}
catch(IOException e1) {}
System.out.println("PropertyLoader: (load)" + e.getMessage());
return false;
}
catch(IOException ioe) {
System.out.println("PropertyLoader: (load)" + ioe.getMessage());
}
pptContainer.put(pptname, ppt);
i++;
}
return true;
}
/**
* 把pathname目录下的所有扩展名为filename的文件加载到到此类中
* @param pathname:property文件所在目录的绝对路径
* @param filename:要装载的文件名(不包括后缀.properties)
* @return 装载成功,返回true; 反之,返回false
*/
public static boolean load(String pathname, String filename)
{
File dir= new File(pathname);
if(!dir.isDirectory()){
System.out.println("PropertyLoader: (load)Not directory");
return false;
}
FileInputStream fis;
try
{
directory = dir.getAbsolutePath();
fis = new FileInputStream(directory + File.separatorChar + filename + ".properties");
}
catch(FileNotFoundException fe)
{
System.out.println("PropertyLoader: (load)" + fe.getMessage());
return false;
}
Properties ppt = new Properties();
try
{
ppt.load(fis);
fis.close();
}
catch(IOException e)
{
try
{
fis.close();
}
catch(IOException e1) {}
System.out.println("PropertyLoader: (load)" + e.getMessage());
return false;
}
if (pptContainer==null){
System.out.println("PropertyLoader: (load)pptContainer is null");
return false;
}
pptContainer.put(filename, ppt);
return true;
}
/**
* 在pptname的文件中根据key找到property的值
* @param pptname:文件名
* @param key:返回值对应的key值
* @return 返回对应key的值
*/
public static String getPptValue(String pptname, String key)
{
Properties ppt = (Properties)pptContainer.get(pptname);
if(ppt == null)
load(directory);
return (String)ppt.get(key);
}
/**
* 设置property的值(可用来设定某些目录的路径,例如,下载文件所放置的目录是/WEB_INF/upload,
* 在pathinfo.properties配置文件中(放置一些目录路径:rootpath,templatepath,uploadpath etc),
* 找到uploadpath,通过此方法设置uploadpath路径的值为/WEB_INF/upload:
* setPptValue("pathinfo","uploadpath","/WEB_INF/upload"))
*
* @param pptname:property的文件名
* @param key:值对应的key
* @param value:被设定的值
* @return true or false
*/
public static boolean setPptValue(String pptname, String key, String value)
{
Properties ppt = (Properties)pptContainer.get(pptname);
if(ppt == null)
{
return false;
} else
{
ppt.put(key, value);
return true;
}
}
/**
* 移除pptname文件中key对应的值
* @param pptname:property的文件名
* @param key:该值对应的key
* @return 返回被移除的值
*/
public static String removePptValue(String pptname, String key)
{
Properties ppt = (Properties)pptContainer.get(pptname);
if(ppt == null)
return null;
else
return (String)ppt.remove(key);
}
/**
* 保存指定目录下的所有property文件
* @param path:这些property文件的存储目录
* @param pptName:不带后缀的文件名
* @return 存储成功返回true;否则,返回false
*/
public static boolean save(String path, String pptName)
{
File dir;
dir = new File(path);
if(!dir.isDirectory()){
System.out.println("PropertyLoader: (save)Not directory");
return false;
}
Properties ppt;
try
{
directory = dir.getAbsolutePath();
}
catch(NullPointerException e)
{
System.out.println("PropertyLoader: (save)" + e.getMessage());
return false;
}
catch(SecurityException e)
{
System.out.println("PropertyLoader: (save)" + e.getMessage());
return false;
}
try {
ppt = (Properties)pptContainer.get(pptName);
FileOutputStream fos = new FileOutputStream(directory + File.separatorChar + pptName + ".properties");
if(ppt != null)
ppt.store(fos, "The properties of " + pptName);
fos.close();
return true;
}catch(FileNotFoundException e) {
System.out.println("PropertyLoader: (save)" + e.getMessage());
return false;
}catch(IOException ioe){
System.out.println("PropertyLoader: (save)" + ioe.getMessage());
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -