首页 > 解决方案 > 格式化java输出

问题描述

我有一个来自 java 类的 O/P。O/P 需要对齐以提高可读性。例如:我在 O/P 中将新行作为字符本身 '\n' 得到,整个 O/P 是一个连续的字符串,没有正确对齐,这使得 O/P 难以阅读。

样品 O/P

'comp.db.actionserver.ActionServerResponse'::{
  action:'comp.db.workflow.actions.GetHostMemAllocInfo'::{
    rawOutput:'java.lang.String'::"meminfo::{\n  replicationStoreReader:'18700'::'malloc'::{\n    max :   9580544,\n    alloc : 6679840,\n    free :  348896,\n    frag :  5,\n  },\n  replicationStoreReader:'18700'::'ConnectionQueue'::{\n    max :   3563520,\n    alloc : 1412928,\n    free :  8384,\n    frag :  0,\n  },\n  replicationStoreReader:'18701'::'malloc'::{\n    max :   6295552,\n    alloc : 5779744,\n    free :  446176,\n    frag :  7,\n  },\n  replicationStoreReader:'18701'::'ConnectionQueue'::{\n    max :   1105920,\n    alloc : 44864,\n    free :  1061056,\n    frag :  2365,\n  },\n  storeReader:'30300'::'ConnectionQueue'::{\n    max :   2105344,\n    alloc : 548176,\n    free :  1557168,\n    frag :  284,\n  },\n  storeReader:'30301'::'malloc'::{\n    max :   20574208,\n    alloc : 19905408,\n    free :  668800,\n    frag :  3,\n  },\n  storeReader:'30301'::'ConnectionQueue'::{\n    max :   2105344,\n    alloc : 339632,\n    free :  1765712,\n    frag :  519,\n  },\n}\n2020-06-01T07:42:43.106253\tmain\t0",
    result:'java.lang.String'::"completed",
    returnMessage:'java.lang.String'::"GetHostMemAllocInfo: Action execution successful"
  },
  message:'java.lang.String'::"Action was executed succesfully",
  status:'comp.db.actionserver.ServiceResponse$Status'::"OK"
}

我正在寻找适当的对齐方式,以便 '\n' 字符实际上提供换行符,并且连续字符串中的制表符空格可用于正确对齐 O/P。像这样的东西

'comp.db.actionserver.ActionServerResponse'::{
  action:'comp.db.workflow.actions.GetHostMemAllocInfo'::{
    rawOutput:'java.lang.String'::
    "meminfo::{
        replicationStoreReader:'18700'::'malloc'::{
            max :   9580544,
            alloc : 6679840,
            free :  348896,
            frag :  5,
        },
        replicationStoreReader:'18700'::'ConnectionQueue'::{
            max :   3563520,
            alloc : 1412928,
            free :  8384,
            frag :  0,
        },
        replicationStoreReader:'18701'::'malloc'::{
            max :   6295552,
            alloc : 5779744,
            free :  446176,
            frag :  7,
        },
        replicationStoreReader:'18701'::'ConnectionQueue'::{
            max :   1105920,
            alloc : 44864,
            free :  1061056,
            frag :  2365,
        },
        storeReader:'30300'::'ConnectionQueue'::{
            max :   2105344,
            alloc : 548176,
            free :  1557168,
            frag :  284,
        },
        storeReader:'30301'::'malloc'::{
            max :   20574208,
            alloc : 19905408,
            free :  668800,
            frag :  3,
        },
        storeReader:'30301'::'ConnectionQueue'::{
            max :   2105344,
            alloc : 339632,
            free :  1765712,
            frag :  519,
        },
    }
    2020-06-01T07:42:43.106253  main  0",
    result:'java.lang.String'::"completed",
    returnMessage:'java.lang.String'::"GetHostMemAllocInfo: Action execution successful"
  },
  message:'java.lang.String'::"Action was executed succesfully",
  status:'comp.db.actionserver.ServiceResponse$Status'::"OK"
}

标签: javaregex

解决方案


你可以这样做: yourString = yourString.replace("\n", System.lineSeparator());

我不确定您是否需要转义**。如果是这样,请使用\\n它应该可以工作。


推荐阅读