Java Write String to File writeFile(File file, String data)

Here you can find the source of writeFile(File file, String data)

Description

Write a String to a file.

License

Apache License

Parameter

Parameter Description
file the file to write
data what to write to the file

Declaration

public static void writeFile(File file, String data) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010, 2012 Institute for Dutch Lexicology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from  w  w w.  j  av a 2s .  c  o  m
 *******************************************************************************/

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

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Main {
    /**
     * The default encoding for opening files.
     */
    private static String defaultEncoding = "utf-8";

    /**
     * Write a String to a file.
     * @param file the file to write
     * @param data what to write to the file
     */
    public static void writeFile(File file, String data) {
        PrintWriter out = openForWriting(file);
        try {
            out.print(data);
        } finally {
            out.close();
        }
    }

    /**
     * Opens a file for writing in the default encoding.
     *
     * Wraps the Writer in a BufferedWriter and PrintWriter for efficient and convenient access.
     *
     * @param file
     *            the file to open
     * @return write interface into the file
     */
    public static PrintWriter openForWriting(File file) {
        return openForWriting(file, defaultEncoding);
    }

    /**
     * Opens a file for writing.
     *
     * Wraps the Writer in a BufferedWriter and PrintWriter for efficient and convenient access.
     *
     * @param file
     *            the file to open
     * @param encoding
     *            the encoding to use, e.g. "utf-8"
     * @return write interface into the file
     */
    public static PrintWriter openForWriting(File file, String encoding) {
        try {
            return new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. writeFile(File file, String content)
  2. writeFile(File file, String content, String charset)
  3. writeFile(File file, String content, String charsetName)
  4. writeFile(File file, String contents)
  5. writeFile(File file, String contents)
  6. writeFile(File file, String data)
  7. writeFile(File file, String data)
  8. writeFile(File file, String data, boolean append)
  9. writeFile(File file, String dataString)