2016-11-25 5 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

私は、その私の必要性...使用する必要がありません仲間を見てください@ – UserAtHome

+0

をsimas_chその後、出力がそのがsimas 26、dhananjay 56のようにする必要があり –

+0

のようになりますどのように提供してください...その名前と値の両方を含める必要があることを意味します... – UserAtHome

関連する問題