Java OutputStreamWriter Write saveText(BufferedWriter writer, String string)

Here you can find the source of saveText(BufferedWriter writer, String string)

Description

Saves the given text to the specified BufferedWriter instance, then closes the writer afterwards.

License

Open Source License

Parameter

Parameter Description
writer a parameter
string a parameter

Exception

Parameter Description
IOException an exception

Declaration

static public void saveText(BufferedWriter writer, String string) throws IOException 

Method Source Code

//package com.java2s;
/*//  w w w .  j  a  va  2 s. co m
 *   __               .__       .__  ._____.           
 * _/  |_  _______  __|__| ____ |  | |__\_ |__   ______
 * \   __\/  _ \  \/  /  |/ ___\|  | |  || __ \ /  ___/
 *  |  | (  <_> >    <|  \  \___|  |_|  || \_\ \\___ \ 
 *  |__|  \____/__/\_ \__|\___  >____/__||___  /____  >
 *                   \/       \/             \/     \/ 
 *
 * Copyright (c) 2006-2011 Karsten Schmidt
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

import java.io.*;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
     * Saves the given text to the specified {@link BufferedWriter} instance,
     * then closes the writer afterwards.
     * 
     * @param writer
     * @param string
     * @throws IOException
     */
    static public void saveText(BufferedWriter writer, String string) throws IOException {
        writer.write(string);
        writer.flush();
        writer.close();
    }

    /**
     * Saves the given text to the specified {@link OutputStream} instance, then
     * closes the underlying writer afterwards.
     * 
     * @param output
     * @param string
     * @throws IOException
     */
    static public void saveText(OutputStream output, String string) throws IOException {
        saveText(createWriter(output), string);
    }

    /**
     * Creates a {@link BufferedWriter} for the given file using UTF-8 encoding.
     * 
     * @param file
     * @return writer instance
     * @throws IOException
     */
    public static BufferedWriter createWriter(File file) throws IOException {
        return createWriter(createOutputStream(file));
    }

    /**
     * Creates a {@link BufferedWriter} for the given {@link OutputStream} using
     * UTF-8 encoding.
     * 
     * @param out
     * @return writer instance
     * @throws IOException
     */
    public static BufferedWriter createWriter(OutputStream out) {
        return createWriter(out, "UTF-8");
    }

    /**
     * Creates a {@link BufferedWriter} for the given {@link OutputStream} and
     * using the specified encoding.
     * 
     * @param out
     *            stream
     * @param encoding
     *            text encoding to use
     * @return writer instance
     * @throws IOException
     */
    public static BufferedWriter createWriter(OutputStream out, String encoding) {
        OutputStreamWriter w = null;
        try {
            w = new OutputStreamWriter(out, encoding);
        } catch (UnsupportedEncodingException e) {
        }
        return new BufferedWriter(w, 0x10000);
    }

    /**
     * Creates an {@link OutputStream} for the given file. If the file extension
     * ends with ".gz" the stream is automatically wrapped in a
     * {@link GZIPOutputStream} as well. Also attempts to create any
     * intermediate directories for the file using
     * {@link #createDirectoriesForFile(File)}.
     * 
     * @param file
     *            output file
     * @return output stream
     * @throws IOException
     */
    static public OutputStream createOutputStream(File file) throws IOException {
        if (file == null) {
            throw new IllegalArgumentException("file can't be null");
        }
        createDirectoriesForFile(file);
        OutputStream stream = new FileOutputStream(file);
        if (file.getName().toLowerCase().endsWith(".gz")) {
            stream = new GZIPOutputStream(stream);
        }
        return stream;
    }

    /**
     * Attempts to create the full path of directories as specified by the given
     * target file.
     * 
     * @param file
     * @return true, if the operation succeeded
     */
    static public boolean createDirectoriesForFile(File file) {
        try {
            String parentName = file.getParent();
            if (parentName != null) {
                File parent = new File(parentName);
                if (!parent.exists()) {
                    parent.mkdirs();
                }
            }
            return true;
        } catch (SecurityException se) {
            System.err.println("No permissions to create " + file.getAbsolutePath());
        }
        return false;
    }
}

Related

  1. saveString(String string, File file, String charsetName)
  2. saveString2File(String s, String filename)
  3. saveStringToFile(String filename, String string)
  4. saveStringToFile(String toSave, String fn)
  5. saveStringToPath(String stringToSave, String pathToSaveTo)
  6. saveTextInFile(File file, String content, String encoding)
  7. saveTxt(OutputStream os, List list)
  8. saveUnicodeStringToFile(final String string, final File file)
  9. saveUtfFileWithBOM(File file, String content)