`
nannan408
  • 浏览: 1792879 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

通用Propterties工具类,兼容读jar同级目录的文件。

 
阅读更多
1.前言。
  properties文件的写方法比较不好用,折中写了个通用类。
2.例子。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取Properties综合类
 * 
 * @author lijn 2014-02-25
 */
public class PropertiesUtil {
	/**
	 * 得到某一个类的路径
	 * 
	 * @param name
	 * @return
	 */
	public static String getPath(Class name) {
		String strResult = null;
		if (System.getProperty("os.name").toLowerCase().indexOf("window") > -1) {
			strResult = name.getResource("/").toString().replace("file:/", "")
					.replace("%20", " ");
		} else {
			strResult = name.getResource("/").toString().replace("file:", "")
					.replace("%20", " ");
		}
		return strResult;
	}

	/**
	 * 读取所有的property
	 * 
	 * @param filename
	 *            properties文件路径
	 * @return 所有的property的集合(map形式)
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> getPropties(String filename) {
		if (null == filename) {
			return null;
		}
		String filePath = getPath(PropertiesUtil.class) + filename;
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
			Map<String, String> map = new HashMap<String, String>();
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String Property = props.getProperty(key);
				map.put(key, Property);
			}
			return map;
			// 关闭资源
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取某个property的值
	 * 
	 * @param filename
	 *            文件名
	 * @param key
	 *            property的key
	 * @return property的value
	 */
	public static String setProp(String filename, String key, String value) {
		if (null == filename || null == key) {
			return null;
		}
		String filePath = getPath(PropertiesUtil.class) + filename;
		Properties props = new Properties();
		FileOutputStream out = null;
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
			out = new FileOutputStream(new File(filePath));
			props.setProperty(key, value);
			props.store(out, "update");
			// 关闭资源
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获取某个property的值
	 * 
	 * @param filename
	 *            文件名
	 * @param key
	 *            property的key
	 * @return property的value
	 */
	public static String getProp(String filename, String key) {
		if (null == filename || null == key) {
			return null;
		}
		String filePath = getPath(PropertiesUtil.class) + filename;
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
			return props.getProperty(key);
			// 关闭资源
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 需要将config.properies文件放到和本类同级目录下面
	 * 
	 * @param filename
	 * @param key
	 * @param value
	 * @return
	 */
	public static String setJarProp(String filename, String key, String value) {
		if (null == filename || null == key) {
			return null;
		}
		String filePath = getPath(PropertiesUtil.class) + filename;
		Properties props = new Properties();
		FileOutputStream out = null;
		InputStream in = null;
		try {
			in = PropertiesUtil.class.getResourceAsStream("config.properties");
			// System.out.println(PropertiesUtil.class.getClassLoader().getResource("config.properties"));
			BufferedInputStream b2 = new BufferedInputStream(in);
			props.load(b2);

			out = new FileOutputStream(filePath);
			props.setProperty(key, value);
			props.store(out, "update");
			// 关闭资源
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 读写jar外的properties文件
	 * 
	 * @param filename
	 * @param key
	 * @param value
	 */
	public static void setExtralProperty(String filename, String key,
			String value) {
		if (null == filename || null == key) {
			return;
		}
		String outPathString = System.getProperty("java.class.path");
		outPathString = outPathString.substring(0, outPathString
				.lastIndexOf("\\") + 1);

		String filePath = outPathString + filename;
		Properties props = new Properties();
		FileOutputStream out = null;
		BufferedInputStream b2 = null;
		try {
			b2 = new BufferedInputStream(new FileInputStream(filePath));
			props.load(b2);

			out = new FileOutputStream(new File(filePath));
			props.setProperty(key, value);
			props.store(out, "update");
			// 关闭资源
		} catch (FileNotFoundException e) {
			setProp(filename,key,value);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null!=out){
				 out.close();
				}
				if(null!=b2){
				b2.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	/**
	 * 以行的形式写ja内的文件
	 * 
	 * @param filename
	 * @param key
	 * @param value
	 */
	public static void writeLineInerFile(String filename, String content) {
		if (null == filename || null == content) {
			return;
		}
		String filePath = getPath(PropertiesUtil.class) + filename;
	
		FileOutputStream out = null;
		BufferedInputStream b2 = null;
		byte[] cont=new byte[4];
		String contentResult="";
		try {
			//先读取源文件的内容,再追加内容
			b2 = new BufferedInputStream(new FileInputStream(filePath));
			int res=b2.read(cont);
			while(res!=-1){
				contentResult=contentResult+new String(cont);
				res=b2.read(cont);
			}
			//追加
			contentResult+=content;
			out = new FileOutputStream(new File(filePath));
			out.write(contentResult.getBytes());
			// 关闭资源
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null!=out){
				 out.close();
				}
				if(null!=b2){
				b2.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


	}
	/**
	 * 以行的形式写jar外的文件
	 * 
	 * @param filename
	 * @param key
	 * @param value
	 */
	public static void writeLineExtralFile(String filename, String content) {
		if (null == filename || null == content) {
			return;
		}
		String outPathString = System.getProperty("java.class.path");
		outPathString = outPathString.substring(0, outPathString
				.lastIndexOf("\\") + 1);

		String filePath = outPathString + filename;
		FileOutputStream out = null;
		BufferedInputStream b2 = null;
		byte[] cont=new byte[1024];
		String contentResult="";
		try {
			//先读取源文件的内容,再追加内容
			b2 = new BufferedInputStream(new FileInputStream(filePath));
			int res=b2.read(cont);
			while(res!=-1){
				contentResult=contentResult+new String(cont);
				res=b2.read(cont);
			}
			//追加
			contentResult+=content;
			out = new FileOutputStream(filePath);
			out.write(contentResult.getBytes());
			// 关闭资源
		} catch (FileNotFoundException e) {
			writeLineInerFile(filename,content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null!=out){
				 out.close();
				}
				if(null!=b2){
				b2.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	/**
	 * 读jar外的properties文件的属性
	 * 
	 * @param filename
	 * @param key
	 * @param value
	 */
	public static String getExtralProperty(String filename, String key) {
		if (null == filename || null == key) {
			return null;
		}
		String outPathString = System.getProperty("java.class.path");
		outPathString = outPathString.substring(0, outPathString
				.lastIndexOf("\\") + 1);

		String filePath = outPathString + filename;
		Properties props = new Properties();
		BufferedInputStream b2=null;
		try {
			 b2 = new BufferedInputStream(new FileInputStream(filePath));
			props.load(b2);
			return props.getProperty(key);
			// 关闭资源
		} catch (FileNotFoundException e) {
		  return getProp(filename,key);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null!=b2){
					b2.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;

	}

	public static void main(String[] args) throws IOException {
		writeLineExtralFile("error.txt","fiowe");

	}
}


3.后记。
   如果是要读jar里面的src直属的properties,直接用ResourseBundle就可以了。
ResourceBundle resourceBundle = ResourceBundle.getBundle(filename
				.replace(".properties", ""));
		String temp = resourceBundle.getString(key);

0
0
分享到:
评论

相关推荐

    Java工具类导出成jar包

    下面将详细介绍如何通过简单的步骤将Java工具类打包成jar文件。 首先,确保你已经在Java集成开发环境(如Eclipse、IntelliJ IDEA等)中编写并编译了你的Java工具类。这些类应该被组织在一个或者多个包(package)中...

    jar文件反编译工具

    Java Archive (JAR) 文件是Java平台上的一个特殊文件格式,用于封装多个类文件、资源文件和其他相关数据。当我们遇到不熟悉的JAR文件,想要学习其中的代码逻辑或查找特定功能实现时,就需要使用到“jar文件反编译...

    工具类的jar包

    自己写的一些工具类的打包 主要是文件操作 文件夹路径获取 xml文件操作 json对象转换

    jarjar-方便Java打包工具,自定义修改jar包包名

    其中,"jarjar"是一个非常实用的工具,专为Java程序员设计,用于方便地对JAR文件进行操作,如重命名类、移动类或者合并多个JAR文件。在本文中,我们将详细探讨jarjar工具的使用方法、功能以及其在实际项目中的应用。...

    Java modbus工具类jlibmodbus-1.2.9.7.jar

    稀缺工具类jar包。主要用于modbus通讯主从站使用。相较于modbus4j,该jar包再网上比较难找到,需要的可以下载使用!既可以配置主站,也可以配置从站,支持TCP和RTU.该工具包适用于eclipse软件,直接再lib中导入即可...

    南大通用数据库驱动 jar 包

    jar文件是一种特殊的文件格式,用于打包Java类库,包括类文件、资源文件等,方便分发和运行。南大通用数据库驱动的jar包内包含了所有必要的类和方法,使得Java程序能够识别并通信于南大通用数据库系统。 南大通用...

    jar文件生成exe文件

    为了解决这个问题,我们可以将JAR文件转换为更通用的可执行文件格式,例如Windows平台下的EXE文件。本文将详细介绍如何将JAR文件生成为EXE文件。 首先,我们需要理解JAR文件和EXE文件的区别。JAR(Java Archive)...

    apk文件中添加jar文件

    Java Archive(JAR)文件是Java平台中用于存储类文件、资源文件和其他相关文件的容器。开发者经常使用JAR文件来打包和分发库,以便在多个项目中复用。 添加JAR文件到APK的步骤如下: 1. **准备JAR文件**:确保你...

    MAC Jar反编译工具

    在IT行业中,Java应用程序通常被打包成JAR(Java Archive)文件,这是一种可执行的文件格式,用于集合Java类、资源和其他相关文件。当需要查看或修改JAR文件内部的源代码时,反编译工具就显得尤为重要。本文将详细...

    java通用jar包

    Java通用jar包是一种在Java开发中广泛使用的可重用代码库,它包含了预编译的类和资源,便于开发者在自己的项目中导入和使用。jar(Java Archive)是Java平台的标准打包格式,允许将多个Java类文件和其他资源文件集合...

    img和jar包,工具类.rar

    总的来说,“img和jar包,工具类.rar”这个压缩包可能包含了一个用于数据操作的Java项目,其中的图像文件可能是用于界面展示,而.jar文件则封装了项目的类库,工具类则提供了数据处理的便利函数。为了进一步了解这个...

    ab文件解析工具abe

    1. **兼容性**:确保你的`abe.jar`版本与创建`.ab`文件的Unity版本兼容,否则可能会出现解析错误。 2. **安全**:因为`.ab`文件可能包含敏感数据,如游戏资产或用户信息,所以在解包和处理这些文件时,务必保持谨慎...

    java解析kml文件工具(JavaAPIforKml-2.2.1.jar)

    JavaAPIforKml-2.2.1.jar 解析kml文件的工具包

    打Jar包小工具,运行cmd.bat自动打jar包

    在Java开发过程中,打包应用程序为JAR(Java Archive)文件是一项常见的任务,它将源代码编译后的类文件和其他资源组合成一个独立的可执行文件。"打Jar包小工具"是一个便捷的解决方案,旨在简化这个过程。这个工具...

    JAR包查看器

    1. **浏览内容**:JAR包查看器允许用户直观地浏览JAR文件中的所有文件和目录结构,包括.class文件和其他资源文件,这对于理解和调试程序的结构非常有帮助。 2. **搜索功能**:通过内置的搜索功能,可以快速定位到...

    jar文件转java查看工具

    在IT行业中,jar文件是一种广泛使用的Java字节码格式,它包含了编译后的Java类、资源文件以及元数据。这些文件通常用于打包和分发Java应用程序或库。然而,有时开发者需要深入到jar文件内部,查看其中的源代码,以...

    错误Jar文件扫描工具

    当你用Mevan进行开发的时候,执行的时候报出一个Jar文件的异常,后面跟着一个长度,这就证明你的jar...你可以利用这个工具类对你的仓库进行扫描,并跳过源码jar包,最后会把无效的jar文件的完整目录及文件名打印出来。

    java实现sftp操作工具类

    3)取文件目录列表 4)取文件列表 5)下载文件 6)复制文件 7)删除文件 8)目录是否存在,文件是否存在 9)移动文件 10)可以基于chnSftp对象进行开发 依赖类包在我的sftp包下载下提供 版权声明:本工具类为个人兴趣基于...

    网络请求okhttputils工具类jar包

    网络请求okhttputils工具类jar包

    编译jar文件学习用

    8. **理解JAR结构**:JAR文件内部的目录结构应遵循Java的包结构,即类文件应按照其完整的类名(包括包名)组织在不同的目录下。 "yuchf"可能是这个压缩包中的一个文件或者目录,这可能是一个特定的示例项目或代码...

Global site tag (gtag.js) - Google Analytics