Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想逐行比较两个属性文件,并分别生成带有修改的、删除的属性的output.txt文件_Java_File_Properties - Fatal编程技术网

Java 我想逐行比较两个属性文件,并分别生成带有修改的、删除的属性的output.txt文件

Java 我想逐行比较两个属性文件,并分别生成带有修改的、删除的属性的output.txt文件,java,file,properties,Java,File,Properties,这段代码是我用来生成的,我得到的输出文件包含修改和删除的属性,但我需要分别使用它们。 基本上输出像 改性性质 “原始属性”-“修改的属性” 删除的属性 “财产” 请帮我摆脱这个困境 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.

这段代码是我用来生成的,我得到的输出文件包含修改和删除的属性,但我需要分别使用它们。 基本上输出像

改性性质

“原始属性”-“修改的属性”

删除的属性

“财产”

请帮我摆脱这个困境

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();
        } 
        }
            }   
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.PrintStream;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Properties;
导入java.util.Set;
公共类FC{
私有属性prop1=null;
私有属性prop2=null;
私有属性prop3=null;
私有属性prop4=null;
公共财政委员会(){
InputStream为1=null;
InputStream为2=null;
InputStream为3=null;
InputStream为4=null;
试一试{
this.prop1=新属性();
is1=新文件输入流(“C:\\new\\pranu.properties”);
项目1.荷载(is1);
this.prop2=新属性();
is2=新文件输入流(“C:\\prop\\pranu1.properties”);
项目2.负载(is2);
this.prop3=新属性();
is3=新文件输入流(“C:\\new\\pranu3.properties”);
项目3.负载(is3);
this.prop4=新属性();
is4=新文件输入流(“C:\\prop\\pranu4.properties”);
项目4.荷载(is4);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共集getAllKeysProp1(){
Set keys=prop1.keySet();
返回键;
}  
公共集getAllKeysProp2(){
Set keys=prop2.keySet();
返回键;
}
公共集getAllKeysProp3(){
Set keys=prop3.keySet();
返回键;
}
公共集getAllKeysProp4(){
Set keys=prop4.keySet();
返回键;
}
公共静态void main(字符串a[]{
FC mpc=新的FC();
列表a1=新的ArrayList();
列表a2=新的ArrayList();
列表a3=新的ArrayList();
列表a4=新的ArrayList();
Set keys=mpc.getAllKeysProp1();
用于(对象k:关键点){
字符串键=(字符串)k;
a1.添加(键);
}
Set keys2=mpc.getAllKeysProp2();
用于(对象k:keys2){
字符串键=(字符串)k;
a2.添加(键);
}
a2.拆除所有(a1);
Set keys3=mpc.getAllKeysProp1();
用于(对象k:keys3){
字符串键=(字符串)k;
a3.添加(键);
}
Set keys4=mpc.getAllKeysProp2();
用于(对象k:keys4){
字符串键=(字符串)k;
a4.添加(键);
}
a3.拆除所有(a4);
a2.addAll(a3);
列表a5=新的ArrayList();
列表a6=新的ArrayList();
列表a7=新的ArrayList();
列表a8=新的ArrayList();
Set keys5=mpc.getAllKeysProp3();
用于(对象k:keys5){
字符串键=(字符串)k;
a5.添加(键);
}    
Set keys6=mpc.getAllKeysProp4();
用于(对象k:keys6){
字符串键=(字符串)k;
a6.添加(键);
}
a6.移除所有(a5);
Set keys7=mpc.getAllKeysProp3();
用于(对象k:keys7){
字符串键=(字符串)k;
a7.添加(键);
}
Set keys8=mpc.getAllKeysProp4();
用于(对象k:keys8){
字符串键=(字符串)k;
a8.添加(键);
}
a7.拆除所有(a8);
a7.添加所有(a6);
System.out.println(a2+“\n”+a7);
System.out.println(“在Props文件夹下的C驱动器中生成的比较报告”);
//首先创建一个文件
试一试{
PrintStream outputfile=新的PrintStream(新文件(“C:\\props\\CR.txt”);
系统放样(输出文件);
String part1=“pranu文件中缺少或修改了这些属性”;
String part2=“这些属性在您的pranu3文件中丢失或修改”;
打印(part1+“\n”+a2+“\n”+part2+“\n”+a7);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
}
}   

我想您希望看到两个系列之间的差异


您可以使用com.google.common.collect.Sets.difference(Set set1,Set set2)

您可以使用
java.util.PropertyResourceBundle
分别加载两个文件

此类提供了将
作为
获取的方法

您可以
迭代
设置
以验证或执行所需的检查


希望这有帮助。

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

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;
  }
 }
公共类属性比较{
公共静态void main(字符串[]args){
字符串propertiesFilename1=System.getProperty(“PropFile1”);
字符串propertiesFilename2=System.getProperty(“PropFile2”);
Map PropFile1=getProps(propertiesFilename1);
Map PropFile2=getProps(propertiesFilename2);
字符串丢失prop=“”;
对于(Map.Entry:PropFile1.entrySet()){
if(PropFile2.containsKey(entry.getKey())){
字符串PropFile2Value=(PropFile2.get(entry.getKey());