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");
- }
- }
- 转自:http://nannan408.iteye.com/blog/2026937
相关推荐
下面将详细介绍如何通过简单的步骤将Java工具类打包成jar文件。 首先,确保你已经在Java集成开发环境(如Eclipse、IntelliJ IDEA等)中编写并编译了你的Java工具类。这些类应该被组织在一个或者多个包(package)中...
在标签中,"Java开发"指明了这些jar包是用于Java编程语言的,"工具类"是指提供通用功能的类库,"代码"则意味着这个集合包含了实现特定功能的源代码。 现在我们来看一下压缩包内的文件名称列表: 1. `mail.jar`:这...
Java Archive (JAR) 文件是Java平台上的一个特殊文件格式,用于封装多个类文件、资源文件和其他相关数据。当我们遇到不熟悉的JAR文件,想要学习其中的代码逻辑或查找特定功能实现时,就需要使用到“jar文件反编译...
自己写的一些工具类的打包 主要是文件操作 文件夹路径获取 xml文件操作 json对象转换
其中,"jarjar"是一个非常实用的工具,专为Java程序员设计,用于方便地对JAR文件进行操作,如重命名类、移动类或者合并多个JAR文件。在本文中,我们将详细探讨jarjar工具的使用方法、功能以及其在实际项目中的应用。...
本话题主要围绕"SFTP的工具类和jar包",我们将深入探讨如何利用jsch-0.1.49.jar库以及SFTPUtil.java工具类进行SFTP操作。 首先,`jsch-0.1.49.jar`是JSch库的一个版本,这是一个Java实现的SSH2库,它提供了对SFTP的...
jar文件是一种特殊的文件格式,用于打包Java类库,包括类文件、资源文件等,方便分发和运行。南大通用数据库驱动的jar包内包含了所有必要的类和方法,使得Java程序能够识别并通信于南大通用数据库系统。 南大通用...
稀缺工具类jar包。主要用于modbus通讯主从站使用。相较于modbus4j,该jar包再网上比较难找到,需要的可以下载使用!既可以配置主站,也可以配置从站,支持TCP和RTU.该工具包适用于eclipse软件,直接再lib中导入即可...
为了解决这个问题,我们可以将JAR文件转换为更通用的可执行文件格式,例如Windows平台下的EXE文件。本文将详细介绍如何将JAR文件生成为EXE文件。 首先,我们需要理解JAR文件和EXE文件的区别。JAR(Java Archive)...
Java Archive(JAR)文件是Java平台中用于存储类文件、资源文件和其他相关文件的容器。开发者经常使用JAR文件来打包和分发库,以便在多个项目中复用。 添加JAR文件到APK的步骤如下: 1. **准备JAR文件**:确保你...
Java通用jar包是一种在Java开发中广泛使用的可重用代码库,它包含了预编译的类和资源,便于开发者在自己的项目中导入和使用。jar(Java Archive)是Java平台的标准打包格式,允许将多个Java类文件和其他资源文件集合...
总的来说,“img和jar包,工具类.rar”这个压缩包可能包含了一个用于数据操作的Java项目,其中的图像文件可能是用于界面展示,而.jar文件则封装了项目的类库,工具类则提供了数据处理的便利函数。为了进一步了解这个...
1. **兼容性**:确保你的`abe.jar`版本与创建`.ab`文件的Unity版本兼容,否则可能会出现解析错误。 2. **安全**:因为`.ab`文件可能包含敏感数据,如游戏资产或用户信息,所以在解包和处理这些文件时,务必保持谨慎...
分布式全局唯一ID生成算法,
当我们想要查看APK中包含的Java类时,就需要先将.dex文件转换为.jar,因为.jar文件可以用Java反编译器来解析。 1. 使用步骤: - 下载dex2jar工具并解压。 - 将APK文件改名为.zip,然后解压缩,找到其中的classes....
jarjar工具便是一个这样的利器,它允许开发者将Java类进行重命名、移动或者打包到新的JAR文件中,以实现更高效、更简洁的依赖管理。本文将详细介绍jarjar-1.4版本的使用环境、功能及操作方法。 一、jarjar-1.4的...
JavaAPIforKml-2.2.1.jar 解析kml文件的工具包
在Java开发过程中,打包应用程序为JAR(Java Archive)文件是一项常见的任务,它将源代码编译后的类文件和其他资源组合成一个独立的可执行文件。"打Jar包小工具"是一个便捷的解决方案,旨在简化这个过程。这个工具...
1. **浏览内容**:JAR包查看器允许用户直观地浏览JAR文件中的所有文件和目录结构,包括.class文件和其他资源文件,这对于理解和调试程序的结构非常有帮助。 2. **搜索功能**:通过内置的搜索功能,可以快速定位到...
在IT行业中,jar文件是一种广泛使用的Java字节码格式,它包含了编译后的Java类、资源文件以及元数据。这些文件通常用于打包和分发Java应用程序或库。然而,有时开发者需要深入到jar文件内部,查看其中的源代码,以...