这个代码我以前生成,我得到的输出文件结合了修改和删除的属性,但我需要单独使用它们。 基本上输出像
修改后的属性
"原始财产" - "修改后的财产"
已删除资产
"属性"
请帮助我解决这个问题
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public class FC {
private Properties prop1 = null;
private Properties prop2 = null;
private Properties prop3 = null;
private Properties prop4 = null;
public FC(){
InputStream is1 = null;
InputStream is2 = null;
InputStream is3 = null;
InputStream is4 = null;
try {
this.prop1 = new Properties();
is1 = new FileInputStream("C:\\new\\pranu.properties");
prop1.load(is1);
this.prop2 = new Properties();
is2 = new FileInputStream("C:\\prop\\pranu1.properties");
prop2.load(is2);
this.prop3 = new Properties();
is3 = new FileInputStream("C:\\new\\pranu3.properties");
prop3.load(is3);
this.prop4 = new Properties();
is4 = new FileInputStream("C:\\prop\\pranu4.properties");
prop4.load(is4);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Set<Object> getAllKeysProp1(){
Set<Object> keys = prop1.keySet();
return keys;
}
public Set<Object> getAllKeysProp2(){
Set<Object> keys = prop2.keySet();
return keys;
}
public Set<Object> getAllKeysProp3(){
Set<Object> keys = prop3.keySet();
return keys;
}
public Set<Object> getAllKeysProp4(){
Set<Object> keys = prop4.keySet();
return keys;
}
public static void main(String a[]){
FC mpc = new FC();
List<String> a1 = new ArrayList<String>();
List<String> a2 = new ArrayList<String>();
List<String> a3 = new ArrayList<String>();
List<String> a4 = new ArrayList<String>();
Set<Object> keys = mpc.getAllKeysProp1();
for(Object k:keys){
String key = (String)k;
a1.add(key);
}
Set<Object> keys2 = mpc.getAllKeysProp2();
for(Object k:keys2){
String key = (String)k;
a2.add(key);
}
a2.removeAll(a1);
Set<Object> keys3 = mpc.getAllKeysProp1();
for(Object k:keys3){
String key = (String)k;
a3.add(key);
}
Set<Object> keys4 = mpc.getAllKeysProp2();
for(Object k:keys4){
String key = (String)k;
a4.add(key);
}
a3.removeAll(a4);
a2.addAll(a3);
List<String> a5 = new ArrayList<String>();
List<String> a6 = new ArrayList<String>();
List<String> a7 = new ArrayList<String>();
List<String> a8 = new ArrayList<String>();
Set<Object> keys5 = mpc.getAllKeysProp3();
for(Object k:keys5){
String key = (String)k;
a5.add(key);
}
Set<Object> keys6 = mpc.getAllKeysProp4();
for(Object k:keys6){
String key = (String)k;
a6.add(key);
}
a6.removeAll(a5);
Set<Object> keys7 = mpc.getAllKeysProp3();
for(Object k:keys7){
String key = (String)k;
a7.add(key);
}
Set<Object> keys8 = mpc.getAllKeysProp4();
for(Object k:keys8){
String key = (String)k;
a8.add(key);
}
a7.removeAll(a8);
a7.addAll(a6);
System.out.println(a2+"\n"+a7);
System.out.println("Comparision Report generated in C Drive under Props folder");
//create a file first
try {
PrintStream outputfile = new PrintStream(new File("C:\\props\\CR.txt"));
System.setOut(outputfile);
String part1 = "These Properties are missed or modified in your pranu file";
String part2 = "These properties are missed or modified in your pranu3 file";
outputfile.print(part1+"\n"+a2+"\n"+part2+"\n"+a7);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您可以使用java.util.PropertyResourceBundle
分别加载两个文件。
此类提供了将keys
用作set
的方法。
您可以iterate
set
进行验证或进行所需的检查。
希望这会有所帮助。
答案 1 :(得分:0)
如果有人还在寻找答案,这是我实现的代码
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;
}
}
答案 2 :(得分:-1)
我想你想看到2个集合之间的差异
你可以使用com.google.common.collect.Sets.difference(Set set1,Set set2)