Java Text File Write writeStringToFile(String contents, String path)

Here you can find the source of writeStringToFile(String contents, String path)

Description

Writes a string to a file, as UTF-8

License

Open Source License

Parameter

Parameter Description
contents The string to write
path The file path

Exception

Parameter Description
IOException In case of failure

Declaration

public static void writeStringToFile(String contents, String path) throws IOException 

Method Source Code


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

import java.io.*;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**//w  w w  . j a v  a2s  . c  o m
     * Writes a string to a file
     *
     * @param contents The string to write
     * @param path The file path
     * @param encoding The encoding to encode in
     * @throws IOException In case of failure
     */
    public static void writeStringToFile(String contents, String path, String encoding) throws IOException {
        OutputStream writer = null;
        if (path.endsWith(".gz")) {
            writer = new GZIPOutputStream(new FileOutputStream(path));
        } else {
            writer = new BufferedOutputStream(new FileOutputStream(path));
        }
        writer.write(contents.getBytes(encoding));
    }

    /**
     * Writes a string to a file, as UTF-8
     *
     * @param contents The string to write
     * @param path The file path
     * @throws IOException In case of failure
     */
    public static void writeStringToFile(String contents, String path) throws IOException {
        writeStringToFile(contents, path, "UTF-8");
    }
}

Related

  1. writeStringToFile(String content, String filePath)
  2. writeStringToFile(String content, String path)
  3. writeStringtoFile(String content, String path)
  4. writeStringToFile(String contents, File outfile)
  5. writeStringToFile(String contents, File outputFile)
  6. writeStringToFile(String contents, String path, String encoding)
  7. writeStringToFile(String data, String filename, boolean append)
  8. writeStringToFile(String data, String filepath)
  9. writeStringToFile(String f, String s, boolean append)