Here you can find the source of dumpUtf8ToFile(List
Parameter | Description |
---|---|
records | List of String to write to file |
delimiter | Delimiter between records |
file | Output file |
Parameter | Description |
---|---|
IOException | an exception |
public static void dumpUtf8ToFile(List<String> records, String delimiter, File file) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; public class Main { /**//from w ww.ja va2 s . c o m * Dump UTF-8 records to the given file using the specified delimiter (or newline if null). * * @param records List of String to write to file * @param delimiter Delimiter between records * @param file Output file * @throws IOException */ // TODO: If operating on larger data sets, it would be better to accept a stream. // TODO use Files and Path public static void dumpUtf8ToFile(List<String> records, String delimiter, File file) throws IOException { byte[] delimiterBytes = (delimiter != null ? delimiter : "\n").getBytes(StandardCharsets.UTF_8); byte[] d = new byte[] {}; try (FileOutputStream output = new FileOutputStream(file)) { for (String line : records) { output.write(d); // Write nothing on the first iteration d = delimiterBytes; output.write(line.getBytes(StandardCharsets.UTF_8)); } } } /** * Dump a UTF-8 string to the given file. * * @param str String to write * @param file Output file * @throws IOException */ public static void dumpUtf8ToFile(String str, File file) throws IOException { dumpUtf8ToFile(Collections.singletonList(str), "", file); } }