【问题标题】:COMPARE 2 or more .PROPERTIES files comparing ONLY keys比较 2 个或更多 .PROPERTIES 文件,仅比较键
【发布时间】:2017-05-04 08:20:57
【问题描述】:

我想比较 2 个或更多(如果可能).properties 文件,它们完全是 i18n 文件。 所以我有默认的messages_es.properties,我首先在其中添加带有值的键,我真正需要的是仅将默认/主messages_es.properties的键与其他.properties文件(例如messages_en.properties)进行比较,以了解哪些翻译留在不同的 .properties 文件中。

基本上:

  • 输入:两个属性文件
  • 输出:第二个 .properties 文件中缺少键

O/P 应该显示第二个 .properties 文件中缺少的键。

【问题讨论】:

标签: java properties compare file-comparison


【解决方案1】:

Properties 类有方法

public synchronized void load(InputStream inStream)
public synchronized void load(Reader reader)

您可以使用它们来加载文件。

然后使用方法

public Set<String> stringPropertyNames()

获取一组属性。

终于Set有了方法

boolean retainAll(Collection<?> c)
boolean removeAll(Collection<?> c)

以不同的方式工作。

【讨论】:

    【解决方案2】:

    如果有人还在寻找答案,我在下面的代码中实现了

    public class PropertiesCompare {
    
     public static void main(String[] args) {
    
        String propertiesFilename1 = System.getProperty("PropFile1");
        String propertiesFilename2 = System.getProperty("PropFile2");
    
        Map<String, String> PropFile1 = getProps(propertiesFilename1);
        Map<String, String> PropFile2 = getProps(propertiesFilename2);
    
        String missingProp = "";
    
        for (Map.Entry<String, String> entry : PropFile1.entrySet()) {
            if (PropFile2.containsKey(entry.getKey())) {
                String PropFile2Value = (PropFile2.get(entry.getKey()));
                String PropFile1Value = (entry.getValue());
    
                if (!PropFile2Value.equalsIgnoreCase(PropFile1Value)) {
                     System.out.println("Key : "+entry.getKey()+"\t\nValue : PropFile1 = "+PropFile1Value+", PropFile2 = "+PropFile2Value);
                }
            } else {
                missingProp= missingProp+(entry.getKey() + " = " + entry.getValue() + "\n");
            }
        }
    }
    
    public static Map<String, String> getProps(String propertiesFilename1) {
        Map<String, String> properties = new HashMap<>();
        try (InputStream stream = new FileInputStream(propertiesFilename1)) {
            Properties prop = new Properties() {
                @Override
                public synchronized Object put(Object key, Object value) {
                    return properties.put(key.toString(), value.toString());
                }
            };
            prop.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
     }
    }
    

    【讨论】:

      【解决方案3】:

      例子(很简单)

      String filename1 = args[0];
      String filename2 = args[1];
      try {
        System.out.println("Load " + filename1);
        Properties prop1 = loadProperties(filename1);
        Properties prop2 = loadProperties(filename2);
      
        Set<String> proprietes1 = prop1.stringPropertyNames();
        Set<String> proprietes2 = prop2.stringPropertyNames();
        proprietes1.removeAll(proprietes2);
        System.out.println("Propriétés de " + filename1 + " qui ne sont pas dans " + filename2);
        for (String prop : proprietes1) {
          System.out.println(prop);
        }
      } catch (FileNotFoundException | IOException e) {
        System.out.println(usage);
        e.printStackTrace();
      } 
      

      地点:

      public static Properties loadProperties(String filename1) throws FileNotFoundException, IOException {
        Properties prop = new Properties();
        BufferedReader reader = new BufferedReader(new FileReader(filename1));
        prop.load(reader);
        return prop;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-16
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        相关资源
        最近更新 更多