2016-11-25 62 views
-1

如何使用CsvWritercsv格式編寫整數數組和字符串數組?CSVWriter對於整數數組

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

如果你只是想寫出一些帶分隔符的數組,你不需要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(",")); 
+0

沒有兄弟我有,因爲它我的需要...使用@ simas_ch – UserAtHome

+0

然後請提供輸出應該什麼樣子 –

+0

它應該像simas 26,dhananjay 56 ...它的意思是它應該包含名稱和值都... – UserAtHome