Here you can find the source of saveToFile(File dest, String contents, Charset cs)
Parameter | Description |
---|---|
dest | destination file |
contents | text to save |
cs | charset |
Parameter | Description |
---|---|
IOException | an exception |
public static void saveToFile(File dest, String contents, Charset cs) throws IOException
//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 } } }