Java Text File Write nio writeLinesToFile(final File file, final String... lines)

Here you can find the source of writeLinesToFile(final File file, final String... lines)

Description

Write lines to a file.

License

Apache License

Parameter

Parameter Description
file the file to write to.
lines the lines to write to the file.

Exception

Parameter Description
IOException if writing to the file fails.

Declaration

private static void writeLinesToFile(final File file, final String... lines) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w w w  .j a  v a 2s .  c o  m
 * Copyright 2014 VU University Medical Center.
 * Licensed under the Apache License version 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.html).
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import java.nio.charset.StandardCharsets;

public class Main {
    /**
     * New line character.
     */
    private static final char NEW_LINE = '\n';

    /**
     * Write lines to a file.
     *
     * @param file  the file to write to.
     * @param lines the lines to write to the file.
     * @throws IOException if writing to the file fails.
     */
    private static void writeLinesToFile(final File file, final String... lines) throws IOException {
        try (final Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
            for (final String line : lines) {
                writer.write(line);
                writer.write(NEW_LINE);
            }
        }
    }
}

Related

  1. writeIfSet(final OutputStream outputStream, final String field, final String value)
  2. writeLine(final CharBuffer line, final CharBuffer output)
  3. writeLines(Collection lines, File file)
  4. writeLines(File file, Iterable lines)
  5. writeLines(String fileName, String[] lines)
  6. writeString(DataOutput dataOutput, String string)
  7. writeString(DataOutput dOut, String value)
  8. writeString(File file, String content)
  9. writeString(final File file, final String str)