首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文件中打印时拆分值

在文件中打印时拆分值
EN

Stack Overflow用户
提问于 2017-01-06 11:46:57
回答 1查看 107关注 0票数 0

我正在打印一个文件中的值,我需要在第一个内容完成后拆分它们,留出一些空格,然后打印下一个

代码语言:javascript
复制
public class Test_Json {


    public static ArrayList<Object> ls1 = new ArrayList<Object>();
    public static ArrayList<Object> ls2 = new ArrayList<Object>();


    public static void main(String[] args) throws Exception {
        JsonParser parser = new JsonParser();
        BufferedWriter bw = null;
        FileWriter fw = null;

        try {
            Gson g = new Gson();
            JsonElement jsonElement1 = parser.parse(new FileReader("D://sample1.json"));
            JsonElement jsonElement2 = parser.parse(new FileReader("D://sample2.json"));
            // System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1,jsonElement2));
            if (!compareJson(jsonElement1, jsonElement2)) {
                Type mapType = new TypeToken<Map<String, Object>>() {
                }.getType();
                Map<String, Object> firstMap = g
                        .fromJson(jsonElement1, mapType);
                Map<String, Object> secondMap = g.fromJson(jsonElement2,
                        mapType);
                System.out.println(" The Two JSON Files Are Not the Same ");
                System.out.println(Maps.difference(firstMap, secondMap));
                String s = Maps.difference(firstMap, secondMap).toString();
                try{
                    fw = new FileWriter("D:\\output.txt");
                    bw = new BufferedWriter(fw);
                    bw.write(s);
                }catch(Exception e){
                    e.printStackTrace();
                }

            } else {
                System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }

    }

    public static boolean compareJson(JsonElement json1, JsonElement json2) {
        boolean isEqual = true;


        if (json1 != null && json2 != null) {


            if (json1.isJsonObject() && json2.isJsonObject()) {
                Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1)
                        .entrySet();
                Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2)
                        .entrySet();
                JsonObject json2obj = (JsonObject) json2;
                if (ens1 != null && ens2 != null
                        && (ens2.size() == ens1.size())) {

                    for (Entry<String, JsonElement> en : ens1) {
                        isEqual = isEqual
                                && compareJson(en.getValue(),
                                        json2obj.get(en.getKey()));
                    }
                } else {
                    return false;
                }
            }

            else if (json1.isJsonArray() && json2.isJsonArray()) {
                JsonArray jarr1 = json1.getAsJsonArray();
                JsonArray jarr2 = json2.getAsJsonArray();
                if (jarr1.size() != jarr2.size()) {
                    return false;
                } else {
                    int i = 0;

                    for (JsonElement je : jarr1) {
                        isEqual = isEqual && compareJson(je, jarr2.get(i));
                        i++;
                    }
                    if (isEqual) {
                        ls1.toArray();
                        ls2.toArray();
                        isEqual = ls1.containsAll(ls2);
                    }
                }
            }

            else if (json1.isJsonNull() && json2.isJsonNull()) {
                return true;
            }

            else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {

                ls1.add(json1);
                ls2.add(json2);
                return true;
            }
            else if((json1.isJsonPrimitive() & json2.isJsonArray()) || (json2.isJsonPrimitive() && json1.isJsonArray())){
                return false;
            }
        } else if (json1 == null && json2 == null) {
            return true;
        } else {
            return false;
        }

        return isEqual;
    }
}

Map.difference方法从这两个文件中获取差异并打印它们。我需要首先打印第一个文件差异,然后留出几行空格,然后打印下一个文件差异。我这样做是为了分别显示内容。目前它是在没有任何空格的情况下显示,因此很难识别。

我需要在第一个文件中使用类似这样的差值:

代码语言:javascript
复制
{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}

第二个文件中的差异:

代码语言:javascript
复制
{
    "id": 14,
    "name": "A green door bell",
    "price": 127.50,
    "tags": ["home", "green"]
}

方法entriesOnlyOnLeft()和entriesOnlyOnRight()没有显示不同的键,因为键应该是不同的,但这里的键是相同的

EN

回答 1

Stack Overflow用户

发布于 2017-01-06 12:03:05

代码语言:javascript
复制
MapDifference diff = Maps.difference(firstMap, secondMap);
bw.write("Only on left: " + diff.entriesOnlyOnLeft());
// add separation
bw.write("Only on right: " + diff.entriesOnlyOnRight());

要包括值差异,请执行以下操作:

代码语言:javascript
复制
bw.write("Value differences: " + diff.entriesDiffering());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41498618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档