Java程序使用属性文件的经验

本文介绍了如何在Eclipse项目中正确配置和使用属性文件,包括适用于单一包和跨包引用的不同方法,并提供了具体的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java类库(java.util.Properties)提供了读取和解析属性文件,它的行格式为name=value,例如:

MySQL.properties
user=user1
password=password1
host=localhost

Java程序可读取该属性文件来获得连接参数。

在命令行运行环境中,属性文件可通过如下方式之一被找到:
    
1. 属性文件位于CLASSPATH环境变量所指明的目录下    
2. 在程序中使用完整路径的路径名来指定它


在Eclipse的工程中,属性文件应该存放在那里才合适呢?以下给出一个推荐做法。


1. 属性文件只被一个package 中的java源文件使用

把属性文件和java源文件放在同一目录(src/com/pkg1/test)并加入工程。build工程时,属性文件会被自动拷贝到bin/your_package目录中。

java源程序调用getResourceAsStream(MySQL.properties)。

2. 属性文件被多个package中的java源文件使用
在src目录下新建res目录,用来存放无需编译的资源。它可以看作为"precompiled class"目录。把属性文件放在res目录中。build工程时,res目录会被自动拷贝到bin目录中。
java源程序调用getResourceAsStream(/res/MySQL.properties)。

Eclipse项目的.classpath定义如下:
<classpath>
<classpathentry kind="src" path="src"/> # 源文件根目录
<classpathentry kind="output" path="bin"/> # .class编译输出的根目录
</classpath>
bin也是Eclipse工程运行时根目录,绝对资源路径都是以它为起点。

可参考JDK手册了解name参数格式
public InputStream getResourceAsStream(String name)


工程示例



代码实例

ReadPropsFile1.java -- 

<span style="font-size:12px;">package com.pkg1.test;

// This program demonstrates how to read a Java properties file to
// get MySQL connection parameters.

//#@ _FRAG_
import java.util.*;   // need this for properties file support

public class ReadPropsFile1
{
	public static void main (String[] args)
	{
	    String url = null;
	    String propsFile = "MySQL.properties";
	    Properties props = new Properties ();
	
	    try
	    {
	      props.load (ReadPropsFile1.class.getResourceAsStream (propsFile));
	    }
	    catch (Exception e)
	    {
	      System.err.println ("Cannot read properties file");
	      System.exit (1);
	    }
	
		// construct connection URL, encoding username
		// and password as parameters at the end
		url = "jdbc:mysql://"
	            + props.getProperty ("host")
	            + "/cookbook"
	            + "?user=" + props.getProperty ("user")
	            + "&password=" + props.getProperty ("password");
	    System.out.println (url);
	}
}
//#@ _FRAG_</span>

ReadPropsFile2.java --

<span style="font-size:12px;">package com.pkg2.test;

// This program demonstrates how to read a Java properties file to
// get MySQL connection parameters.

//#@ _FRAG_
import java.util.*;   // need this for properties file support

public class ReadPropsFile2
{
	public static void main (String[] args)
	{
	    String url = null;
	    String propsFile = "/res/MySQL.properties";
	    Properties props = new Properties ();
	
	    try
	    {
	      props.load (ReadPropsFile2.class.getResourceAsStream (propsFile));
	    }
	    catch (Exception e)
	    {
	      System.err.println ("Cannot read properties file");
	      System.exit (1);
	    }
	
		// construct connection URL, encoding username
		// and password as parameters at the end
		url = "jdbc:mysql://"
	            + props.getProperty ("host")
	            + "/cookbook"
	            + "?user=" + props.getProperty ("user")
	            + "&password=" + props.getProperty ("password");
	    System.out.println (url);
	}
}
//#@ _FRAG_
</span>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值