CsvSaver { public void save(DataFrame df, File file) { if (createMissingDirs) { File dir = file.getParentFile(); if (dir != null) { dir.mkdirs(); } } try (FileWriter out = new FileWriter(file)) { save(df, out); } catch (IOException e) { throw new RuntimeException("Error writing CSV to " + file + ": " + e.getMessage(), e); } } CsvSaver(); CsvSaver format(CSVFormat format); CsvSaver createMissingDirs(); CsvSaver noHeader(); void save(DataFrame df, File file); void save(DataFrame df, String fileName); void save(DataFrame df, Appendable out); String saveToString(DataFrame df);  }
@Test public void testSave_ToWriter() { DataFrame df = DataFrame.newFrame("A", "B").foldByRow( 1, 2, 3, 4); StringWriter out = new StringWriter(); Csv.saver().save(df, out); assertEquals("A,B\r\n" + "1,2\r\n" + "3,4\r\n", out.toString()); }
@Test public void testSave_ToFile() throws IOException { File file = new File(outPath("testToFile.csv")); DataFrame df = DataFrame.newFrame("A", "B").foldByRow( 1, 2, 3, 4); Csv.saver().save(df, file); assertEquals("A,B\r\n" + "1,2\r\n" + "3,4\r\n", readFile(file.getAbsolutePath())); }
@Test public void testSave_ToFilePath() throws IOException { String filePath = outPath("testToFilePath.csv"); DataFrame df = DataFrame.newFrame("A", "B").foldByRow( 1, 2, 3, 4); Csv.saver().save(df, filePath); assertEquals("A,B\r\n" + "1,2\r\n" + "3,4\r\n", readFile(filePath)); }
@Test public void testSave_NoMkdirs() { String path = outPath("NoMkdirs" + File.separator + "f4" + File.separator + "testToFile.csv"); File file = new File(path); DataFrame df = DataFrame.newFrame("A", "B").foldByRow( 1, 2, 3, 4); assertThrows(RuntimeException.class, () -> Csv.saver().save(df, file)); }