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 data to a file.

License

Apache License

Parameter

Parameter Description
file the file to write.
data The content to write to the file.
encoding encoding to use

Exception

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

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Internet2//from ww  w. j  av a2  s  . c om
 * 
 * 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.
 ******************************************************************************/

import java.io.File;

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

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * <p>
     * Writes data to a file. The file will be created if it does not exist.
     * </p>
     * <p>
     * There is no readFileToString 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 encoding to use
     * @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 java.io.FileOutputStream(file);
        try {
            out.write(data.getBytes(encoding));
        } finally {
            closeQuietly(out);
        }
    }

    /**
     * close a writer quietly
     * @param writer
     */
    public static void closeQuietly(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                //swallow, its ok
            }
        }
    }

    /**
     * Unconditionally close an <code>Reader</code>.
     * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
     *
     * @param input A (possibly null) Reader
     */
    public static void closeQuietly(Reader input) {
        if (input == null) {
            return;
        }

        try {
            input.close();
        } catch (IOException ioe) {
        }
    }

    /**
     * Unconditionally close an <code>OutputStream</code>.
     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
     * @param output A (possibly null) OutputStream
     */
    public static void closeQuietly(OutputStream output) {
        if (output == null) {
            return;
        }

        try {
            output.close();
        } catch (IOException ioe) {
        }
    }

    /**
     * Unconditionally close an <code>InputStream</code>.
     * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
     * @param input A (possibly null) InputStream
     */
    public static void closeQuietly(InputStream input) {
        if (input == null) {
            return;
        }

        try {
            input.close();
        } catch (IOException ioe) {
        }
    }
}

Related

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