属性文件对比工具类

/*
  This file is part of BORG.

  BORG is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  BORG is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with BORG; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Copyright 2003 by Mike Berger
 */

package net.sf.borg.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

/**
 *属性文件对比工具类
 *Author:liuwang
 *Creation time:2012年12月17日10:21
 */
public class PropertiesUtil {
	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		String filePath = "c:\\borg_resource.properties";
		String filePath2 = "c:\\borg_resource_zh_CN.properties";
		distinct(filePath2, filePath);
	}

	/**
	 * 对比差异,后者独有属性
	 */
	public static void distinct(String filePath, String filePath2) {
		File file = new File(filePath);
		File file2 = new File(filePath2);
		FileInputStream fis = null;
		FileInputStream fis2 = null;
		try {
			fis = new FileInputStream(file);
			Properties prop = new Properties();
			prop.load(fis);
			fis2 = new FileInputStream(file2);
			Properties prop2 = new Properties();
			prop2.load(fis2);
			// ** 循环读取英文属性文件的key,去中文属性文件中查找,如果没找到就输出*//*
			Iterator itr2 = prop2.entrySet().iterator();
			while (itr2.hasNext()) {
				Entry e2 = (Entry) itr2.next();
				Iterator itr = prop.entrySet().iterator();
				Boolean flag = false;
				while (itr.hasNext()) {
					Entry e = (Entry) itr.next();
					if (e2.getKey().equals(e.getKey())) {
						// System.out.println("相同的key"+e2.getKey());
						flag = true;
						break;
					} else {
					}

				}
				if (!flag)
					System.out.println(e2.getKey() + " = " + e2.getValue());
			}
			sumOfPro(filePath);
			sumOfPro(filePath2);

			// fos2 = new FileOutputStream(file2);
			// prop.store(fos2, null);
		} catch (Exception e) { // TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				fis2.close();
				// fos2.close();
			} catch (IOException e) { // TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 总属性个数
	 * @param filePath
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void sumOfPro(String filePath) throws FileNotFoundException,
			IOException {
		Properties p = new Properties();
		p.load(new FileInputStream(new File(filePath)));
		Iterator itr = p.entrySet().iterator();
		int i = 0;
		while (itr.hasNext()) {
			Entry e = (Entry) itr.next();
			i++;
			// System.out.println(e.getKey());
		}
		System.out.println(filePath + "文件属性总数为:" + i);
	}
	/**
	 * 运行转码
	 * @return
	 */
	public static String toAscii() {
		try {
			Runtime.getRuntime()
					.exec("cmd.exe   /c   start   d:\\java\\jdk1.6\\bin\\native2ascii.exe");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}

你可能感兴趣的:(工具类)