Java Text File Write nio writeFileContent(File file, String content)

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

Description

Writes the content of the string to the (UTF-8 encoded) file.

License

Creative Commons License

Parameter

Parameter Description
file File to write String content to.

Exception

Parameter Description
IOException an exception

Return

Content of file.

Declaration

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

Method Source Code

//package com.java2s;
/** /* www.  j  a  v a  2  s  .c  om*/
This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools).
It is licensed under the Creative Commons Attribution License 
(see http://creativecommons.org/licenses/by/3.0) by 
the YAGO-NAGA team (see http://mpii.de/yago-naga)
    
Some utility methods for arrays
*/

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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

public class Main {
    /**
     * Writes the content of the string to the (UTF-8 encoded) file.
     * 
     * @param file  File to write String content to.
     * @return      Content of file.
     * @throws IOException 
     */
    public static void writeFileContent(File file, String content) throws IOException {
        BufferedWriter writer = getBufferedUTF8Writer(file);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    /**
     * Creates a BufferedWriter for UTF-8-encoded files
     * 
     * @param file  File in UTF-8 encoding
     * @return      BufferedWriter for file
     * @throws FileNotFoundException
     */
    public static BufferedWriter getBufferedUTF8Writer(File file) throws FileNotFoundException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8")));
    }

    /**
     * Creates a BufferedWriter for UTF-8-encoded files
     * 
     * @param fileName  Path to file in UTF-8 encoding
     * @return      BufferedWriter for file
     * @throws FileNotFoundException
     */
    public static BufferedWriter getBufferedUTF8Writer(String fileName) throws FileNotFoundException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), Charset.forName("UTF-8")));
    }
}

Related

  1. writeAscii(CharSequence ascii, OutputStream os)
  2. writeContent(File file, String content)
  3. writeContent(File file, String content)
  4. writeData(String address, BigInteger toWrite)
  5. writeDataFromList(String address, ArrayList toWrite)
  6. writeFileContents(File file, String content)
  7. writeFileTxt(String fileName, String[] totalFile)
  8. writeHeader(DataOutput data, String header)
  9. writeIfSet(final OutputStream outputStream, final String field, final String value)