Here you can find the source of writeStringToFile(String fileName, String str, String charset)
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. |
Parameter | Description |
---|---|
IOException | an exception |
SecurityException | an exception |
public static void writeStringToFile(String fileName, String str, String charset) throws IOException, SecurityException
//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 } } }