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);
分享到:
相关推荐
下面将详细介绍如何通过简单的步骤将Java工具类打包成jar文件。 首先,确保你已经在Java集成开发环境(如Eclipse、IntelliJ IDEA等)中编写并编译了你的Java工具类。这些类应该被组织在一个或者多个包(package)中...
SpringBoot 上传文件到本服务器目录与 jar 包同级问题 SpringBoot 上传文件到本服务器目录与 jar 包同级问题是一个常见的问题。当我们使用 SpringBoot 的 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文件转换为更通用的可执行文件格式,例如Windows平台下的EXE文件。本文将详细介绍如何将JAR文件生成为EXE文件。 首先,我们需要理解JAR文件和EXE文件的区别。JAR(Java Archive)...
在IT行业中,Java应用程序通常被打包成JAR(Java Archive)文件,这是一种可执行的文件格式,用于集合Java类、资源和其他相关文件。当需要查看或修改JAR文件内部的源代码时,反编译工具就显得尤为重要。本文将详细...
总的来说,“img和jar包,工具类.rar”这个压缩包可能包含了一个用于数据操作的Java项目,其中的图像文件可能是用于界面展示,而.jar文件则封装了项目的类库,工具类则提供了数据处理的便利函数。为了进一步了解这个...
3. **dex2jar工具**:在标签中提到的“dex2jar”是一种常用的工具,可以将.dex文件转换为.jar文件。这个过程通常发生在逆向工程或调试Android应用时,因为Java源代码无法直接从.dex文件中获取,而.jar文件则更容易...
分布式全局唯一ID生成算法,
jarjar工具便是一个这样的利器,它允许开发者将Java类进行重命名、移动或者打包到新的JAR文件中,以实现更高效、更简洁的依赖管理。本文将详细介绍jarjar-1.4版本的使用环境、功能及操作方法。 一、jarjar-1.4的...
阴影JAR(Uber JAR)是将多个库合并到一个单一的JAR文件中的过程,这个过程中会重命名或“阴影”内部类,以避免不同库之间的依赖冲突。Flink-shaded-hadoop-2-uber-2.7.5-10.0.jar中包含了对Hadoop的兼容性实现,...
1. **浏览内容**:JAR包查看器允许用户直观地浏览JAR文件中的所有文件和目录结构,包括.class文件和其他资源文件,这对于理解和调试程序的结构非常有帮助。 2. **搜索功能**:通过内置的搜索功能,可以快速定位到...
在IT行业中,jar文件是一种广泛使用的Java字节码格式,它包含了编译后的Java类、资源文件以及元数据。这些文件通常用于打包和分发Java应用程序或库。然而,有时开发者需要深入到jar文件内部,查看其中的源代码,以...
当你用Mevan进行开发的时候,执行的时候报出一个Jar文件的异常,后面跟着一个长度,这就证明你的jar...你可以利用这个工具类对你的仓库进行扫描,并跳过源码jar包,最后会把无效的jar文件的完整目录及文件名打印出来。
3)取文件目录列表 4)取文件列表 5)下载文件 6)复制文件 7)删除文件 8)目录是否存在,文件是否存在 9)移动文件 10)可以基于chnSftp对象进行开发 依赖类包在我的sftp包下载下提供 版权声明:本工具类为个人兴趣基于...
Apache FTP工具所需JAR文件是Java开发中用于与FTP服务器交互的重要组件,这些JAR库提供了方便的API,使得开发者可以轻松地实现FTP文件传输功能。以下是对这些JAR文件的详细说明: 1. `ant-jakarta-oro-1.6.1.jar`: ...
JAR文件是Java平台特有的打包机制,它将多个类文件、资源文件以及元数据等组合在一起,便于分发、存储和执行。在Java开发中,JAR文件常用于创建库、应用程序或部署Web应用。 描述中提到的"将Hello工程打包成Hello....