Just as an info, [configuration] has a similar problem. There is a FileHandler class (in the io subpackage) which allows defining a target file in various ways and does the conversion to streams.
Oliver Am 14.06.2016 um 18:33 schrieb Benedikt Ritter: > How about something like: > > on CSVFormat: > public PrintingOps print() > > And the PrintingOps class would implement all the different printing > methods, combining the CSVFormat and a printer. The call would look like: > > CSVFormat.EXCEL.print().contentsOf(file, StandardCharsets.UTF_8) > > sebb <seb...@gmail.com> schrieb am Di., 14. Juni 2016 um 17:48 Uhr: > >> On 14 June 2016 at 16:27, Gary Gregory <garydgreg...@gmail.com> wrote: >>> On Jun 14, 2016 5:19 AM, "James Carman" <ja...@carmanconsulting.com> >> wrote: >>>> >>>> Are Readers that hard to create? >>> >>> No, but remembering how to do this is a pain: >>> >>> File out = ... >>> >>> Charset charset = ... >>> >>> CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(new >>> FileOutputStream(out), charset), format); >>> Instead of: >>> >>> format.print(file, charset); >>> >>> We can roll these two APIs back out and document that the components only >>> provides low-level APIs, forget convenience APIs. >>> >>> We can also find a better home for these APIs... like where? On the >> printer >>> static side? >> >> Commons IO >> >>> Gary >>> >>> Itv >>>> >>>> On Tue, Jun 14, 2016 at 2:17 AM Gary Gregory <garydgreg...@gmail.com> >>> wrote: >>>> >>>>> On Mon, Jun 13, 2016 at 11:13 PM, Benedikt Ritter <brit...@apache.org >>> >>>>> wrote: >>>>> >>>>>> I don't like how we're evolving CSVFormat. It is becoming a dumping >>>>> ground >>>>>> for anything that may be useful or convenient. The more methods we >>> add, >>>>> the >>>>>> harder it becomes for users to find the right method for their use >>> case. >>>>>> >>>>> >>>>> Small is nice, I get that. But how would you do it differently so >> that I >>>>> can easily use Paths, Files, URI, and URLs... >>>>> >>>>> Gary >>>>> >>>>> >>>>>> >>>>>> Benedikt >>>>>> >>>>>> <ggreg...@apache.org> schrieb am Di., 14. Juni 2016 um 07:53 Uhr: >>>>>> >>>>>>> Author: ggregory >>>>>>> Date: Tue Jun 14 05:53:32 2016 >>>>>>> New Revision: 1748347 >>>>>>> >>>>>>> URL: http://svn.apache.org/viewvc?rev=1748347&view=rev >>>>>>> Log: >>>>>>> Add convenience API CSVFormat.print(File, Charset) (JIRA is down >>> ATM). >>>>>>> >>>>>>> Modified: >>>>>>> commons/proper/csv/trunk/src/changes/changes.xml >>>>>>> >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java >>>>>>> >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java >>>>>>> >>>>>>> Modified: commons/proper/csv/trunk/src/changes/changes.xml >>>>>>> URL: >>>>>>> >>>>>> >>>>> >>> >> http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1748347&r1=1748346&r2=1748347&view=diff >>>>>>> >>>>>>> >>>>>> >>>>> >>> >> ============================================================================== >>>>>>> --- commons/proper/csv/trunk/src/changes/changes.xml (original) >>>>>>> +++ commons/proper/csv/trunk/src/changes/changes.xml Tue Jun 14 >>>>> 05:53:32 >>>>>>> 2016 >>>>>>> @@ -40,6 +40,7 @@ >>>>>>> <body> >>>>>>> <release version="1.5" date="2016-MM-DD" description="Bug fix >>>>>>> release"> >>>>>>> <action issue="CSV-187" type="update" dev="ggregory" >>>>> due-to="Gary >>>>>>> Gregory">Update platform requirement from Java 6 to 7.</action> >>>>>>> + <action issue="CSV-???" type="add" dev="ggregory" >>> due-to="Gary >>>>>>> Gregory">Add convenience API CSVFormat.print(File, >> Charset)</action> >>>>>>> </release> >>>>>>> <release version="1.4" date="2016-05-28" description="Feature >>> and >>>>>> bug >>>>>>> fix release"> >>>>>>> <action issue="CSV-181" type="update" dev="ggregory" >>>>> due-to="Gary >>>>>>> Gregory">Make CSVPrinter.print(Object) GC-free.</action> >>>>>>> >>>>>>> Modified: >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java >>>>>>> URL: >>>>>>> >>>>>> >>>>> >>> >> http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1748347&r1=1748346&r2=1748347&view=diff >>>>>>> >>>>>>> >>>>>> >>>>> >>> >> ============================================================================== >>>>>>> --- >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java >>>>>>> (original) >>>>>>> +++ >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java >>>>>>> Tue Jun 14 05:53:32 2016 >>>>>>> @@ -28,10 +28,14 @@ import static org.apache.commons.csv.Con >>>>>>> import static org.apache.commons.csv.Constants.SP; >>>>>>> import static org.apache.commons.csv.Constants.TAB; >>>>>>> >>>>>>> +import java.io.File; >>>>>>> +import java.io.FileOutputStream; >>>>>>> import java.io.IOException; >>>>>>> +import java.io.OutputStreamWriter; >>>>>>> import java.io.Reader; >>>>>>> import java.io.Serializable; >>>>>>> import java.io.StringWriter; >>>>>>> +import java.nio.charset.Charset; >>>>>>> import java.sql.ResultSet; >>>>>>> import java.sql.ResultSetMetaData; >>>>>>> import java.sql.SQLException; >>>>>>> @@ -864,6 +868,27 @@ public final class CSVFormat implements >>>>>>> } >>>>>>> >>>>>>> /** >>>>>>> + * Prints to the specified output. >>>>>>> + * >>>>>>> + * <p> >>>>>>> + * See also {@link CSVPrinter}. >>>>>>> + * </p> >>>>>>> + * >>>>>>> + * @param out >>>>>>> + * the output >>>>>>> + * @param charset >>>>>>> + * A charset >>>>>>> + * @return a printer to an output >>>>>>> + * @throws IOException >>>>>>> + * thrown if the optional header cannot be >> printed. >>>>>>> + * @since 1.5 >>>>>>> + */ >>>>>>> + public CSVPrinter print(final File out, Charset charset) >> throws >>>>>>> IOException { >>>>>>> + // The FileWriter will be closed when close() is called. >>>>>>> + return new CSVPrinter(new OutputStreamWriter(new >>>>>>> FileOutputStream(out), charset), this); >>>>>>> + } >>>>>>> + >>>>>>> + /** >>>>>>> * Prints the {@code value} as the next value on the line to >>>>> {@code >>>>>>> out}. The value will be escaped or encapsulated >>>>>>> * as needed. Useful when one wants to avoid creating >>> CSVPrinters. >>>>>>> * >>>>>>> >>>>>>> Modified: >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java >>>>>>> URL: >>>>>>> >>>>>> >>>>> >>> >> http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1748347&r1=1748346&r2=1748347&view=diff >>>>>>> >>>>>>> >>>>>> >>>>> >>> >> ============================================================================== >>>>>>> --- >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java >>>>>>> (original) >>>>>>> +++ >>>>>>> >>>>>> >>>>> >>> >> commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java >>>>>>> Tue Jun 14 05:53:32 2016 >>>>>>> @@ -22,9 +22,12 @@ import static org.junit.Assert.assertArr >>>>>>> import static org.junit.Assert.assertEquals; >>>>>>> import static org.junit.Assert.assertFalse; >>>>>>> >>>>>>> +import java.io.File; >>>>>>> import java.io.IOException; >>>>>>> import java.io.StringReader; >>>>>>> import java.io.StringWriter; >>>>>>> +import java.nio.charset.Charset; >>>>>>> +import java.nio.charset.StandardCharsets; >>>>>>> import java.sql.Connection; >>>>>>> import java.sql.DriverManager; >>>>>>> import java.sql.ResultSet; >>>>>>> @@ -38,6 +41,7 @@ import java.util.List; >>>>>>> import java.util.Objects; >>>>>>> import java.util.Random; >>>>>>> >>>>>>> +import org.apache.commons.io.FileUtils; >>>>>>> import org.junit.Assert; >>>>>>> import org.junit.Ignore; >>>>>>> import org.junit.Test; >>>>>>> @@ -728,6 +732,24 @@ public class CSVPrinterTest { >>>>>>> } >>>>>>> >>>>>>> @Test >>>>>>> + public void testPrintToFileWithDefaultCharset() throws >>>>> IOException { >>>>>>> + File file = File.createTempFile(getClass().getName(), >>> ".csv"); >>>>>>> + try (final CSVPrinter printer = >>> CSVFormat.DEFAULT.print(file, >>>>>>> Charset.defaultCharset())) { >>>>>>> + printer.printRecord("a", "b\\c"); >>>>>>> + } >>>>>>> + assertEquals("a,b\\c" + recordSeparator, >>>>>>> FileUtils.readFileToString(file, Charset.defaultCharset())); >>>>>>> + } >>>>>>> + >>>>>>> + @Test >>>>>>> + public void testPrintToFileWithCharsetUtf16Be() throws >>>>> IOException { >>>>>>> + File file = File.createTempFile(getClass().getName(), >>> ".csv"); >>>>>>> + try (final CSVPrinter printer = >>> CSVFormat.DEFAULT.print(file, >>>>>>> StandardCharsets.UTF_16BE)) { >>>>>>> + printer.printRecord("a", "b\\c"); >>>>>>> + } >>>>>>> + assertEquals("a,b\\c" + recordSeparator, >>>>>>> FileUtils.readFileToString(file, StandardCharsets.UTF_16BE)); >>>>>>> + } >>>>>>> + >>>>>>> + @Test >>>>>>> public void testPrintCustomNullValues() throws IOException { >>>>>>> final StringWriter sw = new StringWriter(); >>>>>>> try (final CSVPrinter printer = new CSVPrinter(sw, >>>>>>> CSVFormat.DEFAULT.withNullString("NULL"))) { >>>>>>> >>>>>>> >>>>>>> >>>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> E-Mail: garydgreg...@gmail.com | ggreg...@apache.org >>>>> Java Persistence with Hibernate, Second Edition >>>>> <http://www.manning.com/bauer3/> >>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/> >>>>> Spring Batch in Action <http://www.manning.com/templier/> >>>>> Blog: http://garygregory.wordpress.com >>>>> Home: http://garygregory.com/ >>>>> Tweet! http://twitter.com/GaryGregory >>>>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org >> For additional commands, e-mail: dev-h...@commons.apache.org >> >> > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org