Java UTF8 dumpUtf8ToFile(List records, String delimiter, File file)

Here you can find the source of dumpUtf8ToFile(List records, String delimiter, File file)

Description

Dump UTF-8 records to the given file using the specified delimiter (or newline if null).

License

Apache License

Parameter

Parameter Description
records List of String to write to file
delimiter Delimiter between records
file Output file

Exception

Parameter Description
IOException an exception

Declaration



public static void dumpUtf8ToFile(List<String> records, String delimiter, File file) throws IOException 

Method Source Code


//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);
    }
}

Related

  1. decodePayloadFromUtf8(String utf8string)
  2. decodeUTF8(byte[] bytes)
  3. decodeUtf8(String url)
  4. decUTF8(String s)
  5. dumpStringArray(Path outputFilePath, String[] stringArray)
  6. fromUTF8(byte[] ba)
  7. fromUTF8(byte[] bytes)
  8. fromUTF8(byte[] bytes)
  9. generateRawUTF8Bytes(final String string)