⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propset.java

📁 这是本人学习JAVA操作XML文件时编写的小软件
💻 JAVA
字号:
package com.rochoc.xml.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author luoc
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 * 
 * 读取配置文件
 */
public class PropSet 
{
	/**
	 * 构造函数
	 * 默认为prop.ini
	 */
	private PropSet()
	{
		Path="prop.ini";
		//初始化
		init();
	}
	/**
	 * 构造函数,设置属性文件的路径
	 * @param path 路径
	 */
	private PropSet(String path)
	{
		Path=path;
		//初始化
		init();
	}
	
	/**
	 * 方法名称:init
     * 方法功能:初始化PropSet
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	private void init()
	{
		try
		{
		    File f=new File(Path);
		    Prop=new Properties();
		    Prop.load(new FileInputStream(f)); 
		}catch(IOException ie)
		{
			System.out.println("读取属性文件 "+Path+" 失败,"+ie);
		}
	}
	
	/**
	 * 方法名称:getInstance
     * 方法功能:获取PropSet的唯一实例
     * @return instance
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static PropSet getInstance()
	{
		if(Instance==null)
		{
			return new PropSet();
		}else
		{
			return Instance;
		}
	}
	
	/**
	 * 方法名称:getInstance
     * 方法功能:获取PropSet的唯一实例
     * @param path
     * @return instance
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static PropSet getInstance(String path)
	{
		if(Instance==null)
		{
			return new PropSet(path);
		}else
		{
			return Instance;
		}
	}
	
	/**
	 * 方法名称:getValueAt
     * 方法功能:获取指定属性的值
     * @param key
     * @return value
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static String getValueAt(String key)
	{
		return Prop.getProperty(key);
	}
	
	/**
	 * 方法名称:getProperty
     * 方法功能:获取指定属性的值
     * @param key
     * @return value
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static String getProperty(String key)
	{
		return Prop.getProperty(key);
	}
	
	/**
	 * 方法名称:getAllProperties
     * 方法功能:获取所有的属性
     * @return 所有的属性
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public Enumeration getAllProperties()
	{
		return Prop.propertyNames();
	}
	
	/**
	 * 方法名称:setValueAt
     * 方法功能:设置指定的属性的值,并保存属性文件
     * @param key
     * @param val
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static void setValueAt(String key,String val)
	{
		try
		{
			File f=new File(Path);
			Prop.setProperty(key,val);
			Prop.store(new FileOutputStream(f),"GB2312");
		}catch(IOException ie)
		{
			System.out.println("修改属性文件"+Path+",保存失败"+ie);
		}catch(Exception e)
		{
			System.out.println("设置属性文件值失败,"+e);
		}
	}
	
	/**
	 * 方法名称:setProperty
     * 方法功能:设置指定的属性的值,并保存属性文件
     * @param key
     * @param val
     * @author:luoc
     * @since:1.0
     * 编写时间:2005-02-04
	 */
	public static void setProperty(String key,String val)
	{
		setValueAt(key,val);
	}
	
	/*全局变量*/
	private static String Path=null;//路径
	private static Properties Prop=null;//读取属性文件对象
	private static PropSet Instance=null;//全局唯一对象
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -