Java Text File Write writeString(String s, File output, String encoding)

Here you can find the source of writeString(String s, File output, String encoding)

Description

Writes a string to a file, using the given encoding.

License

BSD License

Parameter

Parameter Description
s string
output output file
encoding file encoding

Declaration

public static void writeString(String s, File output, String encoding) throws IOException 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.io.BufferedReader;
import java.io.File;

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

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;

public class Main {
    /**// w  ww.j  a v a2 s .c om
     * Writes a string to a file, using the given encoding. An existing file is
     * overwritten.
     * 
     * @param s string
     * @param output output file
     * @param encoding file encoding
     */
    public static void writeString(String s, File output, String encoding) throws IOException {
        BufferedReader buffer = new BufferedReader(new StringReader(s));

        FileOutputStream fos = new FileOutputStream(output);
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, encoding));
        for (String nextLine; (nextLine = buffer.readLine()) != null;)
            writer.println(nextLine);
        writer.close();

        buffer.close();
    }
}

Related

  1. writeString(String filePath, String content)
  2. writeString(String par0)
  3. writeString(String path, String key, String value)
  4. writeString(String path, String str)
  5. writeString(String path, String str, boolean append)
  6. writeString(String string, ObjectOutput out)
  7. writeString(String value)
  8. writeString(StringBuilder out, CharSequence value, boolean escapeXML)
  9. writeStringField(String name, String value, JsonGenerator jgen)