Created
March 8, 2018 12:03
-
-
Save nanomad/71c3923388902ef9f672f6a267e1842f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thomsonreuters.solutions.baml.support; | |
import org.apache.commons.csv.CSVFormat; | |
import org.apache.commons.csv.CSVPrinter; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import java.io.Writer; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class FilterTest { | |
private static final String SAMPLE_CSV_FILE = "./sample.csv"; | |
public static void main(String[] args) throws IOException { | |
Map<String, String> data = new LinkedHashMap<>(); | |
data.put("K1", "V1"); | |
data.put("K2", "V2"); | |
data.put("K4", "V4"); | |
data.put("K3", "V3"); | |
Set<String> header = data.keySet(); | |
try ( | |
Writer writer = new StringWriter(0); | |
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT | |
.withHeader(header.toArray(new String[0]))); | |
) { | |
String[] record = header.stream().map(h -> data.getOrDefault(h, "")).toArray(String[]::new); | |
csvPrinter.printRecord(record); | |
csvPrinter.flush(); | |
System.err.println(writer.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment