- 浏览: 391574 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (139)
- java (39)
- linux (9)
- hibernate (2)
- Spring (10)
- Struts2 (1)
- Ext (38)
- Ext + Java (5)
- Oracle (5)
- JavaScript (15)
- 开发工具 (3)
- ajax (2)
- WebSerivice+Spring (2)
- WebService+CXF (2)
- 服务器 (4)
- MQ (1)
- Apache (3)
- axis (3)
- myeclipse,maven (2)
- WebService (4)
- web (11)
- httpinvoke (1)
- 多线程 (3)
- 同步 (1)
- Servlet (2)
- css (2)
- div (2)
- html (1)
- file (2)
- 应用软件 (1)
- myEclipse10 (1)
- mysql (2)
- Extjs4 (2)
- JavaScript css (1)
- mongodb (2)
- socket (6)
- 流媒体 (5)
- 语音技术 (5)
- freeswitch (1)
最新评论
-
白天看黑夜:
Apache Mina Server 2.0 中文参考手册(带 ...
Apache Mina 学习 -
stduPanda:
引用引用[自行车在现场咨询quote]引用引用引用引用引用引用 ...
Errors running builder 'DeploymentBuilder' on project '工程名' -
鱼翔空:
maven3 导入报Plugin execution not ...
CXF自动生成wsdl与xsd文件 -
哈哈哥_Supper:
closeAction:'hide',
Extjs4 tabPanel关闭后打开 cannot read property addcls of null -
哈哈哥_Supper:
var tab1 = tabPanel.add(
...
Extjs4 tabPanel关闭后打开 cannot read property addcls of null
使用J2SE API读取Properties文件的六种方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
-----------------------------------------------------------------------------------------------------------
以下是具体例子:
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.io.OutputStream;
import java.net.URL;
import java.util.Properties;
public class Test {
//path需要文件的绝对路径
public static String readProperties(String fileName, String parameterName) {
String filePath=Test.class.getResource("/")+"";
//替换所获得路径的/bin为/src
//conf/表示配置文件所在的包名,根据不同的包进行修改
String fPath=filePath.replaceAll("/bin","/src")+"conf/"+fileName;
//去掉path前面的file:/
String path = fPath.replaceAll("file:/","");
System.out.println(path);
File file = new File(path);
String value = "";
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(file);
prop.load(fis);
value = prop.getProperty(parameterName);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
public static Properties readProperties2(String fileName, String parameterName) {
String filePath=Test.class.getResource("/")+"";
String fPath=filePath.replaceAll("/bin","/src")+"conf/"+fileName;
String path = fPath.replaceAll("file:/","");
System.out.println(path);
File file = new File(path);
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(file);
prop.load(fis);
// value = prop.getProperty(parameterName);
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
public static void writeProperties(Properties prop,String fileName){
String filePath=Test.class.getResource("/")+"";
String fPath=filePath.replaceAll("/bin","/src")+"conf/"+fileName;
String path = fPath.replaceAll("file:/","");
System.out.println(path);
File file = new File(path);
try {
OutputStream fos = new FileOutputStream(file);
// 调用 Hashtable 的方法 put。
prop.setProperty("jdbc.url", "新的字符串");
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + "x" + "' value");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
String fileName="1.properties";
Properties properties = readProperties2(fileName,"jdbc.url");
writeProperties(properties,fileName);
System.out.println(readProperties(fileName,"jdbc.url"));
}
}
发表评论
-
linux下查找java进程占用CPU过高原因
2018-04-03 12:35 7521. 查找进程 top查看进程占用资源情况 ... -
Apache Mina TCP/UDP功能使用说明
2017-03-14 14:18 1685Apache Mina ... -
WebService CXF客户端调用样例
2013-01-04 14:48 2033如果服务端提供jar包,客户端直接引用jar ... -
ExtJS layout的9种样式风格总结
2012-12-18 10:29 1085文章转载自:http://virgoooos.iteye.co ... -
Zip文件解析(包含中文目录)
2012-12-11 13:22 2377在解析zip文件时,由于默认版本不支持中文目录,导致解析时遇到 ... -
正则表达式详解
2012-10-09 15:13 928转载自:http://edu.yesky.com/ ... -
map 排序
2012-09-28 10:44 1153用于map的值排序的方法,修改SortedSet 的方法即 ... -
java读写txt文件
2012-09-19 18:49 29975import java.io.BufferedReader; ... -
UTF-8 的bom格式处理
2012-09-14 11:54 1645一般通过httpcient请求的内容,有可能返回utf- ... -
java获取当前工程目录
2012-08-23 17:41 1651转载自:http://blog.csdn.net/yan ... -
用sax解析xml文件把读取的内容写入txt
2012-08-17 10:07 2162import java.io.BufferedWrite ... -
div+css实现圆角聊天框
2012-08-14 15:50 4778需要做个聊天样式的对话框,找了半天,终于找到一个比较好的样式, ... -
linux 启动oracle
2012-07-24 10:58 1025转载:http://www.cnitb ... -
java写excel03文件
2012-07-18 00:06 957用java jxl写入excel03文件,下面给出一个简 ... -
JSP页面查询显示常用模式
2012-07-16 21:09 1194全文转载自:http://pharaohsprinc ... -
Servlet调用Spring bean
2012-07-13 16:30 1687servlet中直接调用spring时会出现Nullpo ... -
Json
2012-07-11 11:25 1063原文转载自:http://www.cnblogs.com ... -
java常用英语词汇翻译
2012-06-04 14:07 2407<java常用英语词汇>abstract 抽象的 ... -
JAVA垃圾回收分析&& java虚拟机垃圾回收机制
2012-06-04 11:11 1220JAVA垃圾回收分析&&a ... -
原子变量(AtomicLong, AtomicInteger, AtomicReference)
2012-05-30 11:49 1727原文转载:http://meng ...
相关推荐
java读取properties文件六种方法
java 读取properties文件代码
java读写properties配置文件
JAVA读取properties的值,JAVA读取properties文件,JAVA读取properties的内容
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
读取Properties文件的六种方法 java properties 文件
很全面 很有用。。。里面有对Properties文件进行读取,写入的操作,达到六种方法。。。
java如何读取properties配置文件
java实现properties文件读取
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
J2SE API读取Properties文件六种方法,相当经典。
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
java完美读取properties文件
主要介绍了 Java读取Properties文件几种方法总结的相关资料,需要的朋友可以参考下
解决JAVA读取properties中文乱码问题
读取properties文件返回map
NULL 博文链接:https://xieke90.iteye.com/blog/2233046