Java Text File Write writeStringToFile(String content, String encoding, String fileName)

Here you can find the source of writeStringToFile(String content, String encoding, String fileName)

Description

write String To File

License

Open Source License

Declaration

public static void writeStringToFile(String content, String encoding, String fileName)
            throws IOException, UnsupportedEncodingException 

Method Source Code


//package com.java2s;
/*//from www.j  a va 2s  .c  o  m
 * {{{ header & license
 * GeneralUtil.java
 * Copyright (c) 2004, 2005 Patrick Wright
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */

import java.io.*;

public class Main {
    public static void writeStringToFile(String content, String encoding, String fileName)
            throws IOException, UnsupportedEncodingException {
        File f = new File(fileName);
        FileOutputStream fos = new FileOutputStream(f);
        OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);

        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw);
        pw.print(content);
        pw.flush();
        bw.flush();
        fos.close();
        System.out.println("Wrote file: " + f.getAbsolutePath());
    }
}

Related

  1. writeStringToFile(final String text, final File filePath)
  2. writeStringToFile(String content, File file)
  3. writeStringToFile(String content, File file)
  4. writeStringToFile(String content, File outputfile)
  5. writeStringToFile(String content, File targetFile)
  6. writeStringToFile(String content, String filename)
  7. writeStringToFile(String content, String filename)
  8. writeStringToFile(String content, String fileName)
  9. writeStringToFile(String content, String filename)