Java Text File Write by Charset writeStringToFile(String fileName, String str, String charset)

Here you can find the source of writeStringToFile(String fileName, String str, String charset)

Description

Writes a string to a file, replacing any existing contents, using the platform's default character set.

License

Open Source License

Parameter

Parameter Description
fileName The file to write to, replacing if it already exists.
str The string to write to the file.
charset The character set to use while writing the file.

Exception

Parameter Description
IOException an exception
SecurityException an exception

Declaration

public static void writeStringToFile(String fileName, String str, String charset)
        throws IOException, SecurityException 

Method Source Code


//package com.java2s;
/*/*w  w  w  .  j  a  va  2  s  .c  om*/
 * Copyright (C) 2016 Jason Jackson
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Writes a string to a file, replacing any existing contents, using the platform's default character set.
     *
     * @param fileName The file to write to, replacing if it already exists.
     * @param str The string to write to the file.
     * @param charset The character set to use while writing the file.
     * @throws IOException
     * @throws SecurityException
     */
    public static void writeStringToFile(String fileName, String str, String charset)
            throws IOException, SecurityException {
        File file = new File(fileName);

        if (!Charset.isSupported(charset)) {
            // check the charset and throw an exception if it isn't supported, before we open the output file,
            // so we don't overwrite an existing file and/or leave it on disk when OutputStreamWriter gets mad
            throw new UnsupportedEncodingException(charset);
        }

        try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset)) {
            writer.write(str);

            // the Writer is automatically closed, which flushes it first;
            // the FileOutputStream is closed by the Writer
        }
    }
}

Related

  1. writeString(OutputStream os, String string, CharsetEncoder encoder)
  2. writeString(OutputStream out, String charset, String value)
  3. writeStringToFile(File file, String s, String charset)
  4. writeStringToFile(String fileContent, String filePath, String charset)
  5. writeStringToFile(String filename, String contents, Charset encoding)
  6. writeStringToStream(String string, OutputStream stream, Charset charset)
  7. writeTextToFile(String textToWrite, String fileName, Charset cs)