如何使用CsvWriter
格式的csv
编写整数数组和字符串数组?
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
public class ArrayHash3
{
public static void main(String args[]) throws IOException
{
int[] WorkingDay = new int[13];
int i = 0;
String[] arrString = null;
int[] arrInt = null;
String[] Name = new String[13];
String file="C:\\Users\\Dhananjay Kumar\\Empdetail\\Detail.csv";
HashMap<String[],int[]> hashfunc=new HashMap<String[],int[]>();
CSVReader reader=new CSVReader(new FileReader(file));
String[] read;
while((read = reader.readNext()) !=null)
{
WorkingDay[i]=Integer.parseInt(read[2]);
Name[i]=read[0];
i++;
}
hashfunc.put(Name,WorkingDay);
hashfunc.get(Name);
Set<Entry<String[], int[]>> entrySet = hashfunc.entrySet();
for (Entry entry : entrySet)
{
arrInt = (int[]) entry.getValue();
arrString = (String[]) entry.getKey();
for(int j=0; j<arrInt.length; j++)
{
System.out.println(arrString[j]+":"+arrInt[j]);
}
}
String File1 = "C:\\Users\\Kumar\\Empdetail\\Detail6.csv";
CSVWriter fw = new CSVWriter(new FileWriter(File1, true));
fw.writeAll(arrString+","+arrInt);
fw.close();
}
}
答案 0 :(得分:0)
如果您只想写出一些带分隔符的数组,则不需要CSVWriter。
查看https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
String commaSeparatedNumbers = numbers.stream()
.map(i -> i.toString())
.collect(Collectors.joining(","));