java以逗号分隔格式写输出

时间:2016-04-21 14:19:45

标签: java apache

如何以逗号分隔格式编写以下输出。请帮助解决此问题。

logger.info("total count of ReportGen requests with size <100kB: "+countlt200);
logger.info("# of Non-ReportGEN Requests: "+countNON);
logger.info("total response time of ReportGen requests with size >=100kB: "+gt200ReportGen);
logger.info("total response time of ReportGen requests with size <100kB: "+lt200ReportGen);
logger.info("total response time of all non ReportGen requests: "+nonReportResTime);
logger.info("total size of ReportGen requests with size <100kB: "+lt200ResTime);
logger.info("total size of ReportGen requests with size >=100kB: "+gt200ResTime);
logger.info("total size of all non ReportGen requests: "+nonReportGenSize);

2 个答案:

答案 0 :(得分:2)

如果我猜对了,你只想制作一个以逗号分隔的输出。

如果您使用的是Java 8,则可以使用String.join()方法(请参阅other post)。

或者您可以使用StringBuilderconcatenation。 这是一个带有StringBuilder的示例:

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("total count of ReportGen requests with size <100kB: "+ countlt200);
stringBuilder.append(", # of Non-ReportGEN Requests: "+countNON);
stringBuilder.append(", total response time of ReportGen requests with size >=100kB: "+gt200ReportGen);

// or if you want it more readable for you 
stringBuilder.append("something..something");
stringBuilder.append(",");
stringBuilder.append("something..something");
stringBuilder.append(",");

// aso

logger.info(stringBuilder.toString());

答案 1 :(得分:0)

您可以使用StringJoiner:

StringJoiner joiner = new StringJoiner(SEPARATOR);
joiner.add(value);
joiner.toString();