API将数组转换为CSV

API将数组转换为CSV

本文介绍了Java API将数组转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个int,float,string等数组.是否有任何实用程序API(例如Commons,Guava)会给我一个逗号分隔的字符串?

Suppose I have an array of int, float, string etc. Is there any utility API (e.g. Commons, Guava) that will give me a comma separated string?

像这样

int[] a = {1,2,3,4,5}.
String s = magicAPI.getCSV(a); // s == "1,2,3,4,5";

推荐答案

我在过去.

StringWriter stringWriter = new StringWriter();
int[] a = {1,2,3,4,5};
String[] b = new String[a.length];
for ( int i = 0; i < a.length; i++) {
    b[i] = a[i];
}
CSVWriter csvWriter = new CSVWriter(stringWriter, ",");
csvWriter.writeNext(b);

但是,对于这样一个简单的示例,您可能只想使用StringBuilderfor循环

However, for such a trivial example you might want to just use the a StringBuilder and a for loop

这篇关于Java API将数组转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:39