Java File Read by Charset saveToFile(File dest, String contents, Charset cs)

Here you can find the source of saveToFile(File dest, String contents, Charset cs)

Description

Save text to a file

License

Open Source License

Parameter

Parameter Description
dest destination file
contents text to save
cs charset

Exception

Parameter Description
IOException an exception

Declaration

public static void saveToFile(File dest, String contents, Charset cs) throws IOException 

Method Source Code

//package com.java2s;
/** (C) Copyright 2005 Hewlett-Packard Development Company, LP
    /*from w ww.  j  av  a 2  s. c  om*/
 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.
    
 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
 For more information: www.smartfrog.org
    
 */

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;

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

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.Writer;
import java.nio.charset.Charset;

public class Main {
    /**
     * Save text to a file
     * @param dest destination file
     * @param contents text to save
     * @param cs charset
     * @throws IOException
     */
    public static void saveToFile(File dest, String contents, Charset cs) throws IOException {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        Writer writer = new OutputStreamWriter(out, cs);
        try {
            writer.write(contents);
        } finally {
            writer.close();
        }
    }

    /**
     * Close any open stream; ignore any errors.
     * @param stream
     */
    public static void close(Closeable stream) {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            //ignore
        }
    }
}

Related

  1. loadFile(File logfile, Charset charset)
  2. loadString(File f, Charset charset)
  3. open(File file, Charset charset)
  4. parseNameDelimiterValueNewLineFile( File nameDelimiterValueNewLineFile, String nameValueDelimiter, Charset charset)
  5. saveFile(File file, String content, String charsetName)
  6. setFileText(File file, Charset charset, String text)
  7. setPropertiesVaule(File file, String key, String value, String comments, Charset charset)
  8. setText(File file, Charset charset, String text)
  9. stringToFile(final String s, final File f, final Charset c)