Java Text File Write by Charset writeTextToFile(String textToWrite, String fileName, Charset cs)

Here you can find the source of writeTextToFile(String textToWrite, String fileName, Charset cs)

Description

Writes plain text to a file.

License

Open Source License

Parameter

Parameter Description
textToWrite the text to write into the file
fileName the name of the file to write to, given as absolute path

Return

a handle to the written file

Declaration

public static Path writeTextToFile(String textToWrite, String fileName,
        Charset cs) throws IOException 

Method Source Code

//package com.java2s;
/*/* w  w w .j  a va2  s .c o  m*/
 * 
 * $Revision$ $Date$
 * 
 * This file is part of *** M y C o R e *** See http://www.mycore.de/ for
 * details.
 * 
 * This program is free software; you can use it, redistribute it and / or
 * modify it under the terms of the GNU General Public License (GPL) 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, in a file called gpl.txt or license.txt. If not, write to the
 * Free Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307 USA
 */

import java.io.IOException;

import java.nio.charset.Charset;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.Arrays;

public class Main {
    /**
     * Writes plain text to a file.
     * 
     * @param textToWrite
     *            the text to write into the file
     * @param fileName
     *            the name of the file to write to, given as absolute path
     * @return a handle to the written file
     */
    public static Path writeTextToFile(String textToWrite, String fileName,
            Charset cs) throws IOException {
        Path file = Paths.get(fileName);
        Files.write(file, Arrays.asList(textToWrite), cs,
                StandardOpenOption.CREATE);
        return file;
    }
}

Related

  1. writeStringToFile(File file, String s, String charset)
  2. writeStringToFile(String fileContent, String filePath, String charset)
  3. writeStringToFile(String filename, String contents, Charset encoding)
  4. writeStringToFile(String fileName, String str, String charset)
  5. writeStringToStream(String string, OutputStream stream, Charset charset)