propertiesutil.java

来自「设备资产管理源代码和 公共的函数」· Java 代码 · 共 137 行

JAVA
137
字号
/*
 * author sophie dong
 * Utility methods for action.properties config file
 */
package com.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

import com.test.util.StrFunction;

public class PropertiesUtil {
	public static String ACTION_PROPERTIES = "action.properties";

	public static Map convertPropertyFileToMap(String propertyFileName) {

		Map actionResultMap = new HashMap();
		URL url = PropertiesUtil.class.getClassLoader().getResource(propertyFileName);
		if (null != url) {
			try {
				File propertyFile = new File(java.net.URLDecoder.decode(url.getFile()));
				String propertyString = readFileToString(propertyFile, "BIG5");
				if (null != propertyString)
					actionResultMap = getActionResultMap(propertyString);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return actionResultMap;

	}

	//input  all invoke result types and pages to a hashMap
	public static Map getActionResultMap(String propertyString) {

		Map actionResultMap = new HashMap();
		String[] actions = StrtoArray(propertyString, "\r\n");
		//propertyString.split("\r\n");
		if (null != actions) {
			for (int i = 0, len = actions.length; i < len; i++) {
				String[] actionPropertis = StrtoArray(actions[i], "|");
				//actions[i].split("\\|");
				actionResultMap.put(actionPropertis[0], actionPropertis);
			}
		}
		return actionResultMap;
	}

	public static String[] StrtoArray(String propertyString, String Split) {
		ArrayList Disp = new ArrayList();
		StringTokenizer st = new StringTokenizer(propertyString, Split);
		int i = 0;
		while (st.hasMoreTokens()) {

			String value = (String) st.nextToken();
			if (!value.equals(null))
				Disp.add(i, value);
			i = i + 1;
		}

		String[] array = new String[Disp.size()];
		Disp.toArray(array);

		//				int i = 0;
		//				while (propertyString.endsWith(Split)) {
		//					Disp.add(i, StrFunction.Get_Left(propertyString, Split));
		//					propertyString = StrFunction.Get_Right(propertyString, Split);
		//					i = i + 1;
		//				}

		for (int f = 0; f > Disp.size(); f++) {
			System.out.println((String) Disp.get(f));
		}
		return array;
	}

	public static String readFileToString(File file, String encode) throws IOException {
		InputStream in = null;
		ByteArrayOutputStream out = null;
		String result;
		byte[] buff = new byte[1024];
		try {
			in = new FileInputStream(file);
			out = new ByteArrayOutputStream((int) file.length());
			int read = 0;
			while ((read = in.read(buff)) != -1) {
				out.write(buff, 0, read);
			}
			result = out.toString(encode);
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}
		return result;
	}

	public static Map getResultMap(String result) {
		String[] results = StrtoArray(result, "*");
		//result.split("\\*");
		Map resultMap = new HashMap();
		for (int i = 0, len = results.length; i < len; i++) {
			String[] resultPropertis = StrtoArray(result, "-");
			//results[i].split("\\-");
			resultMap.put(resultPropertis[0].toString(), resultPropertis);
		}
		return resultMap;
	}

	public static void main(String[] args) {
		Map map = convertPropertyFileToMap(ACTION_PROPERTIES);
		for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
			String element = (String) iter.next();
			System.out.println(element.toString());
		}
	}

}

⌨️ 快捷键说明

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