Java Text File Save writeFile(final String aText, final File aFile, final String aEncoding)

Here you can find the source of writeFile(final String aText, final File aFile, final String aEncoding)

Description

For tests only.

License

Open Source License

Declaration

protected static void writeFile(final String aText, final File aFile, final String aEncoding)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2010 Richard Eckart de Castilho.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Contributors:/*from ww  w  .j  av a  2s .c om*/
 *     Richard Eckart de Castilho - initial API and implementation
 ******************************************************************************/

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

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

import java.io.OutputStreamWriter;

import java.io.Writer;

public class Main {
    /**
     * For tests only.
     */
    protected static void writeFile(final String aText, final File aFile, final String aEncoding)
            throws IOException {
        Writer writer = null;
        try {
            aFile.getParentFile().mkdirs();
            writer = new OutputStreamWriter(new FileOutputStream(aFile), aEncoding);
            writer.write(aText);
        } finally {
            close(writer);
        }
    }

    /**
     * Close the given {@link Closeable}.
     *
     * @param aClosable a closable object.
     */
    public static void close(final Closeable aClosable) {
        if (aClosable != null) {
            try {
                aClosable.close();
            } catch (final IOException e) {
                // Ignore
            }
        }
    }
}

Related

  1. writeFile(File target, String content)
  2. writeFile(final byte[] content, final String path)
  3. writeFile(final File destination, final List contents)
  4. writeFile(final File f, final String content)
  5. writeFile(final File outputFile, final String data, final String encoding)
  6. writeFile(final String file, final String text)
  7. writeFile(final String name, final String s)
  8. writeFile(final String nameFile, final List listMsg)
  9. writeFile(final String outputDir, final String response, final String fileName)