All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.utils.PropertiesUtils Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nervousync.utils;

import org.nervousync.commons.Globals;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;

/**
 * 

Properties utilities

* * Current utilities implements features: *
    Read properties from string/file path/URL instance/input stream
*
    Modify properties file
*
    Storage properties instance to target file path
*
*

属性文件工具集

* * 此工具集实现以下功能: *
    从字符串/本地文件/网络文件/输入流中读取属性文件
*
    修改属性文件
*
    将属性文件保存到目标地址
*
* * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Jan 14, 2010 11:47:08 $ */ public final class PropertiesUtils { /** * Logger instance * 日志实例 */ private final static LoggerUtils.Logger LOGGER = LoggerUtils.getLogger(PropertiesUtils.class); /** *

Private constructor for PropertiesUtils

*

属性文件工具集的私有构造方法

*/ private PropertiesUtils() { } /** *

Read properties from string

*

从字符串读取属性文件

* * @param content Properties information string * 属性信息字符串 * * @return Properties instance * 属性信息实例对象 */ public static Properties readProperties(final String content) { Properties properties = new Properties(); if (StringUtils.notBlank(content)) { try (InputStream inputStream = new ByteArrayInputStream(content.getBytes(Charset.forName(Globals.DEFAULT_ENCODING)))) { if (content.startsWith("<")) { properties.loadFromXML(inputStream); } else { properties.load(inputStream); } } catch (IOException e) { properties = new Properties(); } } return properties; } /** *

Read properties from file path

*

从指定路径读取属性文件

* * @param propertiesFilePath Properties file path * 属性文件路径 * * @return Properties instance * 属性信息实例对象 */ public static Properties loadProperties(final String propertiesFilePath) { try { URL url = FileUtils.getURL(propertiesFilePath); return loadProperties(url); } catch (Exception e) { return new Properties(); } } /** *

Read properties from URL instance

*

从网络路径读取属性文件

* * @param url URL instance * 网络路径 * * @return Properties instance * 属性信息实例对象 */ public static Properties loadProperties(final URL url) { InputStream inputStream = null; try { String fileName = url.getFile(); String fileExtName = StringUtils.getFilenameExtension(fileName); inputStream = url.openStream(); return loadProperties(inputStream, fileExtName.equalsIgnoreCase("xml")); } catch (Exception e) { LOGGER.error("Load_Properties_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } return new Properties(); } finally { IOUtils.closeStream(inputStream); } } /** *

Read properties from input stream.

* Attention: users need to call the close method for input stream manually. *

从输入流中读取属性文件。

* 注意:用户必须手动调用输入流的 close 方法。 * * @param inputStream Input stream instance * 输入流实例对象 * @param isXML Data is XML format * 数据是XML格式 * * @return Properties instance * 属性信息实例对象 */ public static Properties loadProperties(final InputStream inputStream, final boolean isXML) { Properties properties = new Properties(); try { if (isXML) { properties.loadFromXML(inputStream); } else { properties.load(inputStream); } return properties; } catch (Exception e) { LOGGER.error("Load_Properties_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } return new Properties(); } } /** *

Modify properties file

*

修改属性文件

* * @param propertiesFilePath Properties file path * 属性文件路径 * @param modifyMap Modify data key-value map * 要修改的键值对数据表 * @param comment Comment string * 备注字符串 * * @return Process result * 处理结果 */ public static boolean modifyProperties(final String propertiesFilePath, final Map modifyMap, final String comment) { try { Properties modifyProperties = loadProperties(propertiesFilePath); modifyProperties(modifyProperties, modifyMap); return storeProperties(modifyProperties, propertiesFilePath, comment); } catch (Exception e) { LOGGER.error("Modify_Properties_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } return Boolean.FALSE; } } /** *

Modify properties file

*

修改属性文件

* * @param properties Properties instance * 属性信息实例对象 * @param modifyMap Modify data key-value map * 要修改的键值对数据表 */ public static void modifyProperties(final Properties properties, final Map modifyMap) { modifyMap.forEach((key, value) -> properties.setProperty(key, StringUtils.isEmpty(value) ? Globals.DEFAULT_VALUE_STRING : value)); } /** *

Storage properties instance to target file path

*

将属性文件保存到目标地址

* * @param properties Properties instance * 属性信息实例对象 * @param propertiesFilePath Properties file path * 属性文件路径 * @param comment Comment string * 备注字符串 * * @return Process result * 处理结果 */ private static boolean storeProperties(final Properties properties, final String propertiesFilePath, final String comment) { FileOutputStream fileOutputStream = null; try { String filePath = propertiesFilePath.substring(0, propertiesFilePath.lastIndexOf(Globals.DEFAULT_PAGE_SEPARATOR)); FileUtils.makeDir(filePath); String fileExtName = StringUtils.getFilenameExtension(propertiesFilePath); fileOutputStream = new FileOutputStream(propertiesFilePath, false); switch (fileExtName.toLowerCase()) { case "xml": properties.storeToXML(fileOutputStream, comment, Globals.DEFAULT_ENCODING); break; case "properties": properties.store(fileOutputStream, comment); break; default: throw new Exception("Properties file error"); } return Boolean.TRUE; } catch (Exception e) { LOGGER.error("Save_Properties_Error"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Stack_Message_Error", e); } return Boolean.FALSE; } finally { IOUtils.closeStream(fileOutputStream); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy