可以把常改动的属性值列入properties文件,或者xml文件
操作数据库连接时,直接进行读取文件,增加许多方便性
以下是两种读取文件的工具类和properties,xml文件模板 ,还有其方法的测试结果
方法一:读取properties文件
jdbc.properties 模板代码
1.jdbc.driverClassName=com.mysql.jdbc.Driver
2.jdbc.url=jdbc\\:mysql\\://localhost\\:3306/jiqimao?useUnicode\\=true&characterEncoding\\=GBK
3.jdbc.username=jiqimao
4.jdbc.password=jiqimao
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\\:mysql\\://localhost\\:3306/jiqimao?useUnicode\\=true&characterEncoding\\=GBK
jdbc.username=jiqimao
jdbc.password=jiqimao
RpropertiesUtils.java
Java代码
1.package utils;
2.
3.
4.import java.io.IOException;
5.import java.io.InputStream;
6.import java.util.Properties;
7.
8./**
9. * 描述:读取properties配置文件
10. * @author 齐继超
11. *
12. */
13.public class RpropertiesUtils {
14.
15. /**
16. * 配置文件名称,注意文件请放到Source Folder根目录下
17. */
18. private static String fileName = "jdbc.properties";
19.
20. /**
21. * 描述:根据key获取所对应的value值
22. * @param key
23. * @return
24. * 作者:齐继超
25. * 时间:Nov 25, 2010
26. */
27. public static String get(String key){
28. return get(fileName,key);
29. }
30.
31.
32. /**
33. * 描述:进行读取properties配置文件
34. * @param fileName
35. * @param key
36. * @return
37. * 作者:齐继超
38. * 时间:Nov 25, 2010
39. */
40. public static String get(String fileName,String key){
41.
42. //根据当前执行线程返回该线程的上下文 ClassLoader
43. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
44. String value = "";
45. try {
46. //加载类获取源,读入输入流
47. InputStream in = classLoader.getResource(fileName).openStream();
48.
49. //定义个持久的属性集
50. Properties properties = new Properties();
51.
52. //从输入流读取属性列表
53. properties.load(in);
54.
55. //用指定的key获取属性列表中的属性值
56. value = properties.getProperty(key);
57. } catch (IOException e) {
58. e.printStackTrace();
59. throw new RuntimeException("读取配置文件出错");
60. }
61. return value;
62. }
63.
.
65.
66.
67.}
package utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 描述:读取properties配置文件
* @author 齐继超
*
*/
public class RpropertiesUtils {
/**
* 配置文件名称,注意文件请放到Source Folder根目录下
*/
private static String fileName = "jdbc.properties";
/**
* 描述:根据key获取所对应的value值
* @param key
* @return
* 作者:齐继超
* 时间:Nov 25, 2010
*/
public static String get(String key){
return get(fileName,key);
}
/**
* 描述:进行读取properties配置文件
* @param fileName
* @param key
* @return
* 作者:齐继超
* 时间:Nov 25, 2010
*/
public static String get(String fileName,String key){
//根据当前执行线程返回该线程的上下文 ClassLoader
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String value = "";
try {
//加载类获取源,读入输入流
InputStream in = classLoader.getResource(fileName).openStream();
//定义个持久的属性集
Properties properties = new Properties();
//从输入流读取属性列表
properties.load(in);
//用指定的key获取属性列表中的属性值
value = properties.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取配置文件出错");
}
return value;
}
}
测试读取properties文件
Java代码
1.package Test;
2.
3.import utils.RpropertiesUtils;
4.
5.public class TestProperties{
6. public static void main(String[] args) {
7.
8. //测试读取properties文件
9. System.out.println(RpropertiesUtils.get("jdbc.url"));
10. //输出结果"jdbc:mysql://localhost:3306/jiqimao?useUnicode=true&characterEncoding=GBK"
11.
12. }
13.}
package Test;
import utils.RpropertiesUtils;
public class TestProperties{
public static void main(String[] args) {
//测试读取properties文件
System.out.println(RpropertiesUtils.get("jdbc.url"));
//输出结果"jdbc:mysql://localhost:3306/jiqimao?useUnicode=true&characterEncoding=GBK"
}
}
方法二:读取xml文件
jdbc.xml 模板
Xml代码
1.
2.
3. 4. 5. 6. 7. 8. 9.
Rxmlutils.java代码
Java代码
1.package utils;
2.
3.import java.io.File;
4.import java.io.FileInputStream;
5.import java.io.IOException;
6.import java.util.HashMap;
7.import java.util.Map;
8.import java.util.Properties;
9.
10./**
11. * 描述:用properties类读写xml文件
12. * @author 齐继超
13. *
14.*/
15.public class RxmlUtils {
16.
17. /**
18. * 定义xml文件的路径
19. */
20. private static String fileName = "final/jdbc.xml";
21.
22.
23. /**
24. * 描述:根据key取出对应value值
25. * @param key
26. * @return
27. * 作者:齐继超
28. * 时间:Nov 26, 2010
29. */
30. public static String get(String key){
31. return get(fileName,key);
32. }
33.
34.
35.
36. /**
37. * 描述:getValue方法的重载
38. * @return
39. * 作者:齐继超
40. * 时间:Nov 26, 2010
41. */
42. public static String get(String fileName,String key){
43.
44. //给定路径名来创建一个新 File 实例。
45. File pFile = new File(fileName);
46.
47. //定义一个读取字节流
48. FileInputStream pInputStream = null;
49. String value = "";
50. try {
51.
52. //将File载入读取
53. pInputStream = new FileInputStream(pFile);
54.
55. //定义一个属性集,将输入流中由 XML 文档所表示的所有属性加载到此属性表
56. Properties p = new Properties();
57. p.loadFromXML(pInputStream);
58.
59. //根据指定的key获取属性列表中的属性值
60. value =p.getProperty(key);
61.
62. } catch (IOException e) {
63. e.printStackTrace();
. throw new RuntimeException("读取xml文件出错");
65. }
66. return value;
67. }
68.
69.
70.
71.
72.}
package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 描述:用properties类读写xml文件
* @author 齐继超
*
*/
public class RxmlUtils {
/**
* 定义xml文件的路径
*/
private static String fileName = "final/jdbc.xml";
/**
* 描述:根据key取出对应value值
* @param key
* @return
* 作者:齐继超
* 时间:Nov 26, 2010
*/
public static String get(String key){
return get(fileName,key);
}
/**
* 描述:getValue方法的重载
* @return
* 作者:齐继超
* 时间:Nov 26, 2010
*/
public static String get(String fileName,String key){
//给定路径名来创建一个新 File 实例。
File pFile = new File(fileName);
//定义一个读取字节流
FileInputStream pInputStream = null;
String value = "";
try {
//将File载入读取
pInputStream = new FileInputStream(pFile);
//定义一个属性集,将输入流中由 XML 文档所表示的所有属性加载到此属性表
Properties p = new Properties();
p.loadFromXML(pInputStream);
//根据指定的key获取属性列表中的属性值
value =p.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取xml文件出错");
}
return value;
}
}
测试读取xml文件
Java代码
1.package Test;
2.
3.import utils.RxmlUtils;
4.
5.public class TestXml{
6. public static void main(String[] args) {
7.
8.
9. //测试读取xml文件
10. System.out.println(RxmlUtils.get("url"));
11. //输出结果"jdbc:mysql://localhost:3306/jiqimao?useUnicode=true&characterEncoding=GBK"
12.
13. }
14.}
package Test;
import utils.RxmlUtils;
public class TestXml{
public static void main(String[] args) {
//测试读取xml文件
System.out.println(RxmlUtils.get("url"));
//输出结果"jdbc:mysql://localhost:3306/jiqimao?useUnicode=true&characterEncoding=GBK"
}
}
2
顶
2
踩
∙11:50
∙浏览 (113)
∙评论 (2)
∙分类: Java
∙相关推荐
评论
2 楼 Jiagoo 昨天 引用
读取有点问题,配置文件应该以开始读到内存中去,不应该每次get的时候都去通过流去读资源文件。
Java代码
1.import java.io.IOException;
2.import java.io.InputStream;
3.import java.util.Properties;
4.
5./**
6. * 描述:读取properties配置文件
7. * @author 齐继超
8. *
9. */
10.public class RpropertiesUtils {
11.
12. /**
13. * 配置文件名称,注意文件请放到Source Folder根目录下
14. */
15. private static String fileName = "jdbc.properties";
16. private static Properties properties = new Properties();
17.
18. /**
19. * 描述:根据key获取所对应的value值
20. * @param key
21. * @return
22. * 作者:齐继超
23. * 时间:Nov 25, 2010
24. */
25. public static String get(String key){
26. return get(fileName,key);
27. }
28.
29. static{
30. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
31. try {
32. //加载类获取源,读入输入流
33. InputStream in = classLoader.getResource(fileName).openStream();
34.
35. //从输入流读取属性列表
36. properties.load(in);
37. } catch (IOException e) {
38. e.printStackTrace();
39. throw new RuntimeException("读取配置文件出错");
40. }
41. }
42. /**
43. * 描述:进行读取properties配置文件
44. * @param fileName
45. * @param key
46. * @return
47. * 作者:齐继超
48. * 时间:Nov 25, 2010
49. */
50. public static String get(String fileName,String key){
51.
52. //用指定的key获取属性列表中的属性值
53. value = properties.getProperty(key);
54. return value;
55. }
56.
57.
58.
59.
60.} 下载本文