简体   繁体   English

Java以逗号分隔格式写入输出

[英]java write the output in comma delimited format

How to write the below output in comma delimited format.Please help on this. 如何以逗号分隔格式编写以下输出。请提供帮助。

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);

If I guess correctly, you want to make only one output, which is comma separated. 如果我猜对了,您只想输出一个输出,以逗号分隔。

If you are using Java 8 you could use the String.join() method (see other post ). 如果您使用的是Java 8,则可以使用String.join()方法(请参阅其他文章 )。

Or you could use a StringBuilder or just concatenation . 或者您可以使用StringBuilder或仅使用concatenation Here an example with a StringBuilder : 这是一个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());

You can use StringJoiner: 您可以使用StringJoiner:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM