Java BufferedWriter Write writeFileText(File file, String text)

Here you can find the source of writeFileText(File file, String text)

Description

Writes the text data to the specified output file.

License

BSD License

Parameter

Parameter Description
file file path
text text data

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void writeFileText(File file, String text) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
// New BSD License

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

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

import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Main {
    private static final String DEFAULT_ENCODING = "UTF8";

    /**//  w w  w  .  ja  v  a 2  s. c o m
    * Writes the text data to the specified output file.
    *
    * @param file file path
    * @param text text data
    * @throws FileNotFoundException
    * @throws IOException
    */
    public static void writeFileText(File file, String text) throws FileNotFoundException, IOException {
        OutputStream out = new FileOutputStream(file);
        writeLinesCommon(out, text);
    }

    private static void writeLinesCommon(OutputStream out, String text) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(out, DEFAULT_ENCODING));
            writer.write(text);
        } finally {
            close(writer);
        }
    }

    private static void close(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException ex) {
            }
        }
    }
}

Related

  1. writeFile(String p_name, String p_encoding, String p_content)
  2. writeFileAppend(String filePath, String content)
  3. writeFileContent(File audioFile, short[] data, boolean littleEndian)
  4. writeFileContents(File file, String contents)
  5. writeFilenames(String folderPath, LinkedList listOfPopulationFiles)
  6. writeFileToString(String filePath, String fileContent, String encoding, boolean append)