Java Text File Write writeStringToFile(File file, String data, String encoding)

Here you can find the source of writeStringToFile(File file, String data, String encoding)

Description

Writes a String to a file creating the file if it does not exist.

License

Apache License

Parameter

Parameter Description
file the file to write
data the content to write to the file
encoding the encoding to use, null means platform default

Exception

Parameter Description
IOException in case of an I/O error
UnsupportedEncodingException if the encoding is not supported bythe VM

Declaration

public static void writeStringToFile(File file, String data, String encoding) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w ww. j a  v a2s  .c o m*/
 * Copyright 2014 Hyberbin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Email:hyberbin@qq.com
 */

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    /**
     * <p>
     * Writes a String to a file creating the file if it does not exist.
     * </p>
     * <p>
     * There is no writeStringToFile method without encoding parameter because
     * the default encoding can differ between platforms and therefore results
     * in inconsistent results.
     * </p>
     *
     * @param file the file to write
     * @param data the content to write to the file
     * @param encoding the encoding to use, null means platform default
     * @throws IOException in case of an I/O error
     * @throws UnsupportedEncodingException if the encoding is not supported by
     * the VM
     */
    public static void writeStringToFile(File file, String data, String encoding) throws IOException {
        OutputStream out = new FileOutputStream(file);
        try {
            write(data, out, encoding);
        } finally {
            closeQuietly(out);
        }
    }

    public static void write(String data, OutputStream output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(data.getBytes(encoding));
            }
        }
    }

    public static void write(String data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data.getBytes());
        }
    }

    public static void write(char[] data, OutputStream output, String encoding) throws IOException {
        if (data != null) {
            if (encoding == null) {
                write(data, output);
            } else {
                output.write(new String(data).getBytes(encoding));
            }
        }
    }

    public static void write(char[] data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(new String(data).getBytes());
        }
    }

    public static void write(byte[] data, OutputStream output) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }

    public static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    /**
     * Unconditionally close an <code>InputStream</code>.
     * <p>
     * Equivalent to {@link InputStream#close()}, except any exceptions will be
     * ignored. This is typically used in finally blocks.
     *
     * @param input the InputStream to close, may be null or already closed
     */
    public static void closeQuietly(InputStream input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}

Related

  1. writeStringToFile(File file, String content)
  2. writeStringToFile(File file, String content)
  3. writeStringToFile(File file, String contents)
  4. writeStringToFile(File file, String data, String encoding)
  5. writeStringToFile(File file, String data, String encoding)
  6. writeStringToFile(File file, String data, String encoding)
  7. writeStringToFile(File file, String s)
  8. writeStringToFile(File file, String str, boolean compress)
  9. writeStringToFile(File file, String string)