Java FileOutputStream Create writeFileUTF8(final File file, final String contents)

Here you can find the source of writeFileUTF8(final File file, final String contents)

Description

Write the contents of the string out to a file in UTF-8 format.

License

Open Source License

Parameter

Parameter Description
file the file to write to.
contents the contents of the file to write to.

Exception

Parameter Description
IOException if there is an issue writing the file.
NullPointerException if the file parameter is null.

Declaration

public static void writeFileUTF8(final File file, final String contents) throws IOException 

Method Source Code

//package com.java2s;
/*//www . j  a  v a 2s  .  c  om
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 * Portions Copyrighted 2013 ConnId
 * Portions Copyrighted 2015 ForgeRock AS.
 */

import java.io.File;

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

import java.io.OutputStreamWriter;

import java.io.Writer;

public class Main {
    public static final String UTF8 = "UTF-8";

    /**
     * Write the contents of the string out to a file in UTF-8 format.
     *
     * @param file
     *            the file to write to.
     * @param contents
     *            the contents of the file to write to.
     * @throws IOException
     *             if there is an issue writing the file.
     * @throws NullPointerException
     *             if the file parameter is null.
     */
    public static void writeFileUTF8(final File file, final String contents) throws IOException {
        final Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8);
        try {
            writer.write(contents);
        } finally {
            writer.close();
        }
    }
}

Related

  1. writeFileToByte(byte[] bytes, File file)
  2. writeFileToDisk(ByteArrayOutputStream is, String savePath)
  3. writeFileToDisk(File file, String targetPath)
  4. writeFileToFile(File inFile, File toFile)
  5. writeFileToOutputStream(ZipInputStream zipInputStream, File outputFile)
  6. writeFileWithParent(File file, String contentStr, String charset)
  7. writeFileWithParent(String filename, byte[] content)
  8. writeFileWithParent(String filename, String contentStr, String charset)