Skip to content

Commit 347c872

Browse files
committed
Port some code from IO to NIO APIs
1 parent 30329ab commit 347c872

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.io.Writer;
4242
import java.nio.charset.Charset;
4343
import java.nio.charset.StandardCharsets;
44+
import java.nio.file.Files;
45+
import java.nio.file.Path;
4446
import java.sql.BatchUpdateException;
4547
import java.sql.Connection;
4648
import java.sql.DriverManager;
@@ -92,6 +94,14 @@ private static String printable(final String s) {
9294

9395
private final String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
9496

97+
private File createTempFile() throws IOException {
98+
return createTempPath().toFile();
99+
}
100+
101+
private Path createTempPath() throws IOException {
102+
return Files.createTempFile(getClass().getName(), ".csv");
103+
}
104+
95105
private void doOneRandom(final CSVFormat format) throws Exception {
96106
final Random r = new Random();
97107

@@ -1543,7 +1553,7 @@ public void testPrintRecordsWithResultSetOneRow() throws IOException, SQLExcepti
15431553

15441554
@Test
15451555
public void testPrintToFileWithCharsetUtf16Be() throws IOException {
1546-
final File file = File.createTempFile(getClass().getName(), ".csv");
1556+
final File file = createTempFile();
15471557
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, StandardCharsets.UTF_16BE)) {
15481558
printer.printRecord("a", "b\\c");
15491559
}
@@ -1552,7 +1562,7 @@ public void testPrintToFileWithCharsetUtf16Be() throws IOException {
15521562

15531563
@Test
15541564
public void testPrintToFileWithDefaultCharset() throws IOException {
1555-
final File file = File.createTempFile(getClass().getName(), ".csv");
1565+
final File file = createTempFile();
15561566
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, Charset.defaultCharset())) {
15571567
printer.printRecord("a", "b\\c");
15581568
}
@@ -1561,11 +1571,11 @@ public void testPrintToFileWithDefaultCharset() throws IOException {
15611571

15621572
@Test
15631573
public void testPrintToPathWithDefaultCharset() throws IOException {
1564-
final File file = File.createTempFile(getClass().getName(), ".csv");
1565-
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file.toPath(), Charset.defaultCharset())) {
1574+
final Path file = createTempPath();
1575+
try (final CSVPrinter printer = CSVFormat.DEFAULT.print(file, Charset.defaultCharset())) {
15661576
printer.printRecord("a", "b\\c");
15671577
}
1568-
assertEquals("a,b\\c" + recordSeparator, FileUtils.readFileToString(file, Charset.defaultCharset()));
1578+
assertEquals("a,b\\c" + recordSeparator, new String(Files.readAllBytes(file), Charset.defaultCharset()));
15691579
}
15701580

15711581
@Test

0 commit comments

Comments
 (0)