Java Text File Save writeFile(String sName, String data, String encoding)

Here you can find the source of writeFile(String sName, String data, String encoding)

Description

write File

License

Apache License

Declaration

public static void writeFile(String sName, String data, String encoding) 

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./* w  ww.  j a  v a  2  s  .  c om*/
 *******************************************************************************/

import java.io.BufferedWriter;

import java.io.File;

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

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

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) {
        try (PrintWriter out = openForWriting(file, "utf-8")) {
            out.print(data);
        }
    }

    public static void writeFile(String sName, String data, String encoding) {
        File file = new File(sName);
        try (PrintWriter out = openForWriting(file, encoding)) {
            out.print(data);
        }
    }

    /**
     * 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 {
            // If it exists: delete it
            if (file.exists())
                file.delete();
            // Write content to it
            return new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)));
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. writeFile(String path, Vector contents)
  2. writeFile(String pContent, String pPath)
  3. writeFile(String ruta, String nombre, byte[] archivo)
  4. WriteFile(String sFileName, String content)
  5. writeFile(String sFileName, String sContent)
  6. writeFile(String str, File f)
  7. writeFile(String str, String filename, boolean append)
  8. writeFile(String string, File file)
  9. writeFile(String string, File location, boolean forceASCII)