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

📄 propertiesutil.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.util;

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

/**
 * Misc java.util.Properties utils.
 */
public final class PropertiesUtil {

	// ---------------------------------------------------------------- to/from files

	/**
	 * Create properties from the file.
	 *
	 * @param fileName properties file name to load
	 *
	 * @exception IOException
	 */
	public static Properties createFromFile(String fileName) throws IOException {
		return createFromFile(new File(fileName));
	}

	/**
	 * Create properties from the file.
	 *
	 * @param file properties file to load
	 *
	 * @exception IOException
	 */
	public static Properties createFromFile(File file) throws IOException {
		Properties prop = new Properties();
		loadFromFile(prop, file);
		return prop;
	}

	/**
	 * Loads properties from the file. Properties are appended to the existing
	 * properties object.
	 *
	 * @param p        properties to fill in
	 * @param fileName properties file name to load
	 *
	 * @exception IOException
	 */
	public static void loadFromFile(Properties p, String fileName) throws IOException {
		loadFromFile(p, new File(fileName));
	}

	/**
	 * Loads properties from the file. Properties are appended to the existing
	 * properties object.
	 *
	 * @param p      properties to fill in
	 * @param file   file to read properties from
	 *
	 * @exception IOException
	 */
	public static void loadFromFile(Properties p, File file) throws IOException {
		if (p == null) {
			return;
		}
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			p.load(fis);
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
	}


	/**
	 * Writes properties to a file.
	 *
	 * @param p        properties to write to file
	 * @param fileName destination file name
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, String fileName) throws IOException {
		writeToFile(p, new File(fileName), null);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p        properties to write to file
	 * @param fileName destiantion file name
	 * @param header   optional header
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, String fileName, String header) throws IOException {
		writeToFile(p, new File(fileName), header);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p      properties to write to file
	 * @param file   destination file
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, File file) throws IOException {
		writeToFile(p, file, null);
	}

	/**
	 * Writes properties to a file.
	 *
	 * @param p      properties to write to file
	 * @param file   destiantion file
	 * @param header optional header
	 *
	 * @exception IOException
	 */
	public static void writeToFile(Properties p, File file, String header) throws IOException {
		if (p == null) {
			return;
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			p.store(fos, header);
		} finally {
			if (fos != null) {
				fos.close();
			}
		}
	}


	// ---------------------------------------------------------------- properties

	/**
	 * Creates new Properties object from the original one, only by copying
	 * those properties that have specific first part of the key name.
	 *
	 * @param p      Source properties, from which new object will be created
	 * @param base   Base properties key names.
	 *
	 * @return subset properties
	 */
	public static Properties getSubset(Properties p, String base) {
		if (p == null) {
			return null;
		}
		if (StringUtil.isEmpty(base) == true) {
			return p;
		}
		base += ".";
		Properties res = new Properties();
		Iterator iti = p.keySet().iterator();
		int baseLen = base.length();
		while (iti.hasNext()) {
			String key = (String)iti.next();
			if (key.startsWith(base) == true) {
				res.put(key.substring(baseLen), p.get(key));
			}
		}
		return res;
	}

}

⌨️ 快捷键说明

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