Skip to content

Instantly share code, notes, and snippets.

@yashk
Created September 18, 2020 15:20
Show Gist options
  • Save yashk/5330867fa5669ca380b5c2fe80415395 to your computer and use it in GitHub Desktop.
Save yashk/5330867fa5669ca380b5c2fe80415395 to your computer and use it in GitHub Desktop.

Revisions

  1. yashk created this gist Sep 18, 2020.
    41 changes: 41 additions & 0 deletions TestCsv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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));
    }
    }