现在的位置: 首页 > 综合 > 正文

java里面的property文件

2017年08月19日 ⁄ 综合 ⁄ 共 4057字 ⁄ 字号 评论关闭

一、在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们连接本地的数据库那么 IP ,数据库名称,表名称,数据库主机等信息是我们本地的,要使得这个操作数据的模块具有通用性,那么以上信息就不能写死在程序里。通常我们的做法是用配置文件来解决。

   各种语言都有自己所支持的配置文件类型。比如 python ,他支持 .ini 文件。因为他内部有一个 ConfigParser 类来支持 .ini 文件的读写,根据该类提供的方法程序员可以

    自由的来操作 .ini 文件。而在 Java 中, Java 支持的是 .properties 文件的读写。 JDK 内置的 Java.util.Properties 类为我们操作 .properties 文件提供了便利。

        一. .properties 文件的形式

        # 以下为服务器、数据库信息

        dbPort = localhost

        databaseName = mydb

        dbUserName = root

        dbPassword = root

        # 以下为数据库表信息

        dbTable = mytable

        # 以下为服务器信息

        ip = 192.168.0.9

        在上面的文件中我们假设该文件名为: test.properties 文件。其中 # 开始的一行为注释信息;在等号“ = ”左边的我们称之为 key ;等号“ = ”右边的我们称之为 value(其实就是我们常说的键 - 值对) key 应该是我们程序中的变量。而 value 是我们根据实际情况配置的。

        二. JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:

        1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

        2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有

        键 - 值对。以供 getProperty ( String key) 来搜索。

        3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。

        4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输

        出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

        5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

        有了以上几个方法我们就可以对 .properties 文件进行操作了! 

三、property文件的存放:

Java的properties文件需要放到classpath下面,这样程序才能读取到,有关classpath实际上就是Java类或者库的存放路径,在Java工程中,properties放到class文件一块。在web应用中,最简单的方法是放到web应用的WEB-INF\classes目录下即可,也可以放在其他文件夹下面,这时候需要在设置classpath环境变量的时候,将这个文件夹路径加到classpath变量中,这样也也可以读取到。在此,你需要对classpath有个深刻理解,classpath绝非系统中刻意设定的那个系统环境变量,WEB-INF\classes其实也是,Java工程的class文件目录也是

四、property 中文乱码的问题

property文件保存中文时myeclipse会报错,这时我们需要修改资源文件的编码格式。Windons---->Preferences---->Content
Type------>Text----->JavaPropertiesFile,把其Default encoding改为“utf-8”,按“update”更新

Property
文件中,使用的编码方式根据机器本身的设置可能是GBK或者UTF-8。而在Java程序中读取Property文件的时候使用的是Unicode编码方 式,这种编码方式不同会导致中文乱码。因此需要将Property文件中的中文字符转化成Unicode编码方式才能正常显示中文。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * @author zxw
 * 
 */
public class GetSaveProp {
	// public void GetP() {
	// Properties props = new Properties();
	// try {
	// // 资源文件存放在类文件的根目录下。即是放在src下面。
	// props.load(getClass().getClassLoader().getResourceAsStream(
	// "GetProp.properties"));
	// // 当资源文件中有中文的时候可以采用下面的编码转化方法来读取。
	// // 或者使用native2ascii jin1.properties或者jin1.txt
	// // jin.properties将资源文件进行编码转化,
	// // 然后直接读取props.getProperty("name");
	// System.out.println(new String(props.getProperty("name").getBytes(
	// "ISO-8859-1"), "GBK"));
	// } catch (UnsupportedEncodingException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	private void saveProperty() {
		// 保存文件
		Properties propertie = new Properties();
		String characterString = "1中国的";
		propertie.setProperty("character", characterString);
		propertie.setProperty("date", new Date().toString());

		String fileName = "savetest.properties";
		String description = "CharaterTest";
		try {
			FileOutputStream outputFile = new FileOutputStream(fileName);
			propertie.store(outputFile, description);// property类关键的store方法
			outputFile.close();
			// propertie.list(System.out);
			System.out.println("File was saved!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new GetSaveProp().saveProperty();//save file

		// read from property
		Properties readProps = new Properties();
		FileInputStream inStream;
		try {
			inStream = new FileInputStream("savetest.properties");
			readProps.load(inStream);// read from fileinputStream
			// props.list(System.out);
			if (readProps.get("character") != null) {
				System.out.println("character="
						+ new String(readProps.getProperty("character")
								.getBytes("ISO-8859-1"), "UTF-8"));
				System.out.println("character="
						+ new String(readProps.getProperty("character")
								.getBytes("UTF-8"), "UTF-8"));

			} else {
				System.out.println(readProps.get("character"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

抱歉!评论已关闭.