Created
September 18, 2020 15:20
-
-
Save yashk/5330867fa5669ca380b5c2fe80415395 to your computer and use it in GitHub Desktop.
csv with ^A
This file contains hidden or 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
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class TestCsv { | |
private static final String CSV_FILE_NAME = "hive_out.csv"; | |
private static String delimiter = "\u0001"; | |
public static void main(String[] args) throws IOException{ | |
List<String[]> dataLines = new ArrayList<>(); | |
dataLines.add(new String[] | |
{ "John", "Doe", "38", "Manager" }); | |
dataLines.add(new String[] | |
{ "Jane", "Doe", "40", "CTO" }); | |
dataLines.add(new String[] | |
{ "Mark", "Smith", "42", "CFO" }); | |
createCsv(dataLines,CSV_FILE_NAME); | |
} | |
public static void createCsv(List<String[]> dataLines,String fileName) throws IOException { | |
File csvOutputFile = new File(fileName); | |
try (PrintWriter pw = new PrintWriter(csvOutputFile)) { | |
dataLines.stream() | |
.map(TestCsv::convertToCSV) | |
.forEach(line -> pw.println(line)); | |
} | |
} | |
public static String convertToCSV(String[] data) { | |
return Stream.of(data) | |
.collect(Collectors.joining(delimiter)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment