Java Text File Write nio writeString(final File file, final String str)

Here you can find the source of writeString(final File file, final String str)

Description

Writes a string to a file.

License

Open Source License

Parameter

Parameter Description
file The file.
str The string to write.

Exception

Parameter Description
IOException I/O Exception

Declaration

public static void writeString(final File file, final String str) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

public class Main {
    /** The UTF-8 charset. */
    public static final Charset UTF8 = Charset.forName("UTF-8");

    /**//from  w  ww  . j a v a 2s  .  c  om
     * Writes a string to the output stream. The output stream is flushed and
     * closed afterwards.
     * 
     * @param out The output stream.
     * @param str The string to write.
     * @throws IOException I/O Exception
     */
    public static void writeString(final OutputStream out, final String str) throws IOException {
        final Writer writer = new OutputStreamWriter(out, UTF8);
        writer.append(str);
        writer.flush();
        writer.close();
    }

    /**
     * Writes a string to a file.
     * 
     * @param file The file.
     * @param str The string to write.
     * @throws IOException I/O Exception
     */
    public static void writeString(final File file, final String str) throws IOException {
        writeString(new FileOutputStream(file), str);
    }
}

Related

  1. writeLines(String fileName, String[] lines)
  2. writeLinesToFile(final File file, final String... lines)
  3. writeString(DataOutput dataOutput, String string)
  4. writeString(DataOutput dOut, String value)
  5. writeString(File file, String content)
  6. writeString(OutputStream buffer, String s)
  7. writeStringToFile(File file, String content, String value)
  8. writeStringToFile(File file, String string)
  9. writeStringToFile(File file, String text)