Java FileWriter Write saveFileFromString(File file, String encoding, String content)

Here you can find the source of saveFileFromString(File file, String encoding, String content)

Description

writes string to a file

License

Open Source License

Parameter

Parameter Description
file java.io.File object to write string to
encoding file encoding. can be set to null
content file content as a String

Exception

Parameter Description
IOException an exception

Declaration

public static void saveFileFromString(File file, String encoding, String content) throws IOException 

Method Source Code


//package com.java2s;
/*/* w ww  . j av  a  2  s . c o  m*/
_______________________
Apatar Open Source Data Integration
Copyright (C) 2005-2007, Apatar, Inc.
info@apatar.com
195 Meadow St., 2nd Floor
Chicopee, MA 01013
    
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
    
### This program 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 General Public License for more details.
    
### You should have received a copy of the GNU General Public License along
### with this program; if not, write to the Free Software Foundation, Inc.,
### 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
________________________
    
 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;
import java.io.IOException;

import java.io.OutputStreamWriter;

public class Main {
    /**
     * writes string to a file
     * 
     * @param file
     *            java.io.File object to write string to
     * @param encoding
     *            file encoding. can be set to null
     * @param content
     *            file content as a String
     * @throws IOException
     */
    public static void saveFileFromString(File file, String encoding, String content) throws IOException {
        if (content == null) {
            return;
        }
        char[] buf = new char[content.length()];
        content.getChars(0, buf.length, buf, 0);
        OutputStreamWriter f = encoding == null ? new FileWriter(file)
                : new OutputStreamWriter(new FileOutputStream(file), encoding);
        try {
            f.write(buf);
        } finally {
            f.close();
        }
    }
}

Related

  1. saveFile(String path, String content)
  2. SaveFile(String path, String data, boolean deleteOnExit)
  3. saveFile(String path, String sb)
  4. saveFile(String[] lines, String savedName, String extension)
  5. saveFileContents(File file, String contents)
  6. saveFiles(final List contents, final File outputDirectory, final String outputName, final String fileExtension)
  7. saveFlows(Map differences, String file)
  8. saveFormedClustersToFile(Vector names, Collection> clusters, String filename)
  9. saveGraph(List> graph, String sFileName)