Java FileOutputStream Write saveStringIntoFile(File file, String contents)

Here you can find the source of saveStringIntoFile(File file, String contents)

Description

save a string into a file, file does not have to exist

License

Apache License

Parameter

Parameter Description
file is the file to save to
contents is the contents of the file

Declaration

public static void saveStringIntoFile(File file, String contents) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Internet2//  ww  w.j  a v  a  2 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 {
    /**
     * save a string into a file, file does not have to exist
     * 
     * @param file
     *          is the file to save to
     * @param contents
     *          is the contents of the file
     */
    public static void saveStringIntoFile(File file, String contents) {
        try {
            writeStringToFile(file, contents, "ISO-8859-1");
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    /**
     * <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. saveResouce(String resourceName, String outputFile)
  2. saveStream(InputStream is, File output)
  3. saveStream(InputStream stream, File targetFile)
  4. saveStreamToFile(InputStream in, File outFile)
  5. saveStreamToFile(InputStream is, File destFile)
  6. saveStringIntoFile(String filePath, String contents)
  7. saveStringsList(String file, Collection c)
  8. saveStringToFile(File file, String content, String charset)
  9. saveTemp(final byte[] data, final String suffix)