J2SE API读取Properties文件六种方法
http://webservices.ctocio.com.cn/115/8689615.shtml
1。使用Java.util.Properties类的load()方法
示例:InputStreamin=lnewBufferedInputStream(newFileInputStream(name));
Propertiesp=newProperties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例:InputStreamin=newBufferedInputStream(newFileInputStream(name));
ResourceBundlerb=newPropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例:InputStreamin=JProperties.class.getResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:InputStreamin=JProperties.class.getClassLoader().getResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:InputStreamin=ClassLoader.getSystemResourceAsStream(name);
Propertiesp=newProperties();
p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStreamin=context.getResourceAsStream(path);
Propertiesp=newProperties();
p.load(in);
这个类的作用在于帮你解决连接数据库时的" 硬编码" 问题, 即驱动类, 连接字符串, 用户名, 密码这些信息可以通过资源文件来获得, 这种做法既增加了安全性, 又使代码容易维护.
处理数据库资源文件的类 DBConfig.java
这个类的作用在于帮你解决连接数据库时的" 硬编码" 问题, 即驱动类, 连接字符串, 用户名, 密码这些信息可以通过资源文件来获得, 这种做法既增加了安全性, 又使代码容易维护.
处理数据库资源文件的类 DBConfig.java
- import java.util.Properties;
- import java.io.*;
-
- public class DBConfig {
- private static Object initLock = new Object();
-
- private static DBConfig dbconfig = null ;
-
- private Properties props = null ;
-
- public static DBConfig getInstance() {
- if (dbconfig == null ) {
- synchronized (initLock) {
- if (dbconfig == null ) {
- dbconfig = new DBConfig();
-
}
-
}
-
}
- return dbconfig;
-
}
-
- private synchronized void loadProperties() {
- props = new Properties();
- try {
- System.out.println("Load pro file" );
- InputStream in = getClass().getResourceAsStream("/db.properties" );
-
props.load(in);
- } catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
- public String getProperty(String propName) {
- if (props == null ) {
-
loadProperties();
-
}
- return props.getProperty(propName);
-
}
-
}
- import java.util.Properties;
-
import java.io.*;
-
-
public class DBConfig {
-
private static Object initLock = new Object();
-
-
private static DBConfig dbconfig = null;
-
-
private Properties props = null;
-
-
public static DBConfig getInstance() {
-
if (dbconfig == null) {
-
synchronized (initLock) {
-
if (dbconfig == null) {
-
dbconfig = new DBConfig();
- }
- }
- }
-
return dbconfig;
- }
-
-
private synchronized void loadProperties() {
-
props = new Properties();
-
try {
-
System.out.println("Load pro file");
-
InputStream in = getClass().getResourceAsStream("/db.properties");
- props.load(in);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
public String getProperty(String propName) {
-
if (props == null) {
- loadProperties();
- }
-
return props.getProperty(propName);
- }
- }
import java.util.Properties;
import java.io.*;
public class DBConfig {
private static Object initLock = new Object();
private static DBConfig dbconfig = null;
private Properties props = null;
public static DBConfig getInstance() {
if (dbconfig == null) {
synchronized (initLock) {
if (dbconfig == null) {
dbconfig = new DBConfig();
}
}
}
return dbconfig;
}
private synchronized void loadProperties() {
props = new Properties();
try {
System.out.println("Load pro file");
InputStream in = getClass().getResourceAsStream("/db.properties");
props.load(in);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getProperty(String propName) {
if (props == null) {
loadProperties();
}
return props.getProperty(propName);
}
}
资源文件 db.properties
url=jdbc:mysql://localhost:3306/example
driver=org.gjt.mm.mysql.Driver
username=root
password=123654
|
在连接数据库的代码中 , 可以通过以下方式得到驱动类 ,url,username,password
- String driver = DBConfig.getInstance().getProperty( "driver" );
- String url = DBConfig.getInstance().getProperty("url" );
- String username = DBConfig.getInstance().getProperty("username" );
- String password = DBConfig.getInstance().getProperty("password" );
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="0" height="0" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="src" value="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"> <embed type="application/x-shockwave-flash" width="0" height="0" src="http://peaklui.iteye.com/javascripts/syntaxhighlighter/clipboard.swf"></embed></object>
- String driver = DBConfig.getInstance().getProperty("driver");
-
String url = DBConfig.getInstance().getProperty("url");
-
String username = DBConfig.getInstance().getProperty("username");
-
String password = DBConfig.getInstance().getProperty("password");
String driver = DBConfig.getInstance().getProperty("driver");
String url = DBConfig.getInstance().getProperty("url");
String username = DBConfig.getInstance().getProperty("username");
String password = DBConfig.getInstance().getProperty("password");
P.S.
请注意这三个文件的位置 , 建议放在同一个目录下
一.Properties 简介
java.util.Properties 继承自 java.util.Hashtable
Properties 类表示一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串.
二.基本方法
2.1 如何从输入流中加载属性文件
使用load(InputStream is)方法:
-
Properties properties = new Properties();
-
InputStream is = new FileInputStream("conn.properties");
-
properties.load(is);
-
is.close();
2.2 如何读属性文件中的值
使用getProperties(String key)方法:
-
String temp = properties.getProperties(String key);
<注>重载的方法getProperties(String key, String default)方法 将在查询不到值的情况下,返回default.
即: 如果 null == properties.getProperties(String key);
则有 default == properties.getProperties(String key, String default);
2.3 如何获取属性文件中的所有的键
使用propertyNames()方法,该方法返回是键的枚举.
-
Enumeration enumeration = properties.propertyNames();
2.4 如何修改属性文件中的值
使用
-
setProperties(String key, String value)
方法.
<注>该方法调用的 Hashtable 的put方法.如果键存在,则修改值;如果键不存在,则添加值.
2.5 如何存储属性文件到输出流
使用store(OutputStream os, String description)方法:
-
Properties properties = new Properties();
-
OutputStream os = new FileOutputStream("test.properties");
-
String description = "store properties to test.properties";
-
properties.store(os, description);
-
os.close();
2.6 如何清空所有值
使用
方法.
<注>该方法继承自 Hashtable 的clear()方法.清空哈希表.
分享到:
相关推荐
java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...
在本教程中,我们将深入探讨如何在Java中读取属性文件,并结合日志记录进行操作。 首先,我们需要了解如何创建一个简单的属性文件。例如,我们创建一个名为`config.properties`的文件,内容如下: ``` database....
Java 提供了两种方式来操作属性文件,一种是使用 Properties 类,另一种是使用 ResourceBundle 类。 Properties 类是 Java 中的一个内置类,用于读取和写入属性文件。Properties 类提供了多种方法来读取和写入属性...
在Java 7中,文件操作得到了显著的改进,引入了`java.nio.file`包,其中最重要的新特性之一就是Path类的出现。Path类是Java 7文件系统接口(FileSystem API)的核心部分,它提供了一种抽象的方式来表示文件或目录的...
此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作
"Java中用于操作Excel格式文件的类库"指的是利用特定的Java库来读取、写入或者修改Excel文件。在这个描述中,提到了一个名为"jexcelapi"的开源扩展类库,它允许开发者在Java应用中便捷地处理Excel 2003格式的数据。 ...
Java IO文件操作类与复制文件实例,有关文本编辑、FileWriter文件写入、FileReade文件读娶DataStream文件数据流、BufferedReader文件暂存器、FileViewer文件浏览器、复制文件Copy等方面的操作要领,仅供学习参考。
Java文件操作封装类
# java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...
文件工具类java操作文件工具类java操作文件工具类java操作文件工具类java操作文件工具类
这个类是Java标准库的一部分,专门用于处理属性文件。它提供了一组方法,用于加载、保存、设置和获取属性文件中的键值对。 **加载属性文件** 加载属性文件有两种主要方式:从类路径或文件系统中读取。 1. **从类...
这篇博客文章“属性文件读写操作类”可能详细介绍了如何在Java中进行属性文件的操作,包括读取和写入。下面将详细阐述相关知识点。 1. **属性文件格式** 属性文件是纯文本文件,其内容通常以UTF-8编码,每一行表示...
java文件操作工具类是java中针对文件操作的一个工具类,可以方便的读取,下载,上传文件等操作,希望可以帮到大家。
"Java操作Excel文件" Java操作Excel文件是指使用Java语言来读取和写入Excel文件的内容。下面是关于Java操作Excel文件的知识点总结: 一、读取Excel文件内容 在Java中,读取Excel文件内容可以使用jxl.jar这个开源...
java的POI操作Excel文件,java的POI操作Excel文件,java的POI操作Excel文件
在Java编程环境中,处理Excel文件是一项常见的任务,...以上就是关于Java操作Excel文件的基本知识点,涵盖读取、写入以及工作表管理等内容。实际开发中,还需要根据具体需求调整和优化代码,如错误处理、性能优化等。
通过它JAVA开发人员可以读取EXCEL文件的内容,创建新的EXCEL文件,更新已经存在的EXCEL文件。
- **File类**: `java.io.File`是Java中最基础的文件操作类,可以用来表示文件或者目录路径。通过`new File(String path)`构造一个File对象,然后可以调用如`exists()`, `mkdir()`, `delete()`, `renameTo(File dest...
首先,我们了解Java中进行文件操作的基础类:`java.io.File`。这个类是文件和目录路径名的抽象表示,它提供了许多与文件和目录相关的操作方法。 1. **文件对象的创建**: - 使用`new File(String path)`构造函数...