Java File Read by Charset setText(File file, Charset charset, String text)

Here you can find the source of setText(File file, Charset charset, String text)

Description

Write a string to a text file, using the specified encoding.

License

Open Source License

Parameter

Parameter Description
file the file to write.
charset the character set to use for the encoding of the file.
text the string to write into the file.

Return

true if the operation succeeded completely; false otherwise.

Declaration

public static synchronized boolean setText(File file, Charset charset, String text) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;
import java.io.InputStream;

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

import java.io.Writer;

import java.nio.charset.Charset;

import java.util.jar.JarFile;

import java.util.zip.ZipFile;

public class Main {
    public static Charset utf8 = Charset.forName("UTF-8");

    /**//from  ww w  .j  av  a  2  s  .  co m
     * Write a string to a text file using the UTF-8 encoding.
     * @param file the file to write.
     * @param text the string to write into the file.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static boolean setText(File file, String text) {
        return setText(file, utf8, text);
    }

    /**
     * Write a string to a text file, using the specified encoding, or
     * UTF-8 if the specified encoding is not supported.
     * @param file the file to write.
     * @param encoding the name of the charset to use.
     * @param text the string to write into the file.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static boolean setText(File file, String encoding, String text) {
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (Exception ex) {
            charset = utf8;
        }
        return setText(file, charset, text);
    }

    /**
     * Write a string to a text file, using the specified encoding.
     * @param file the file to write.
     * @param charset the character set to use for the encoding of the file.
     * @param text the string to write into the file.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static synchronized boolean setText(File file, Charset charset, String text) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
            bw.write(text, 0, text.length());
            bw.flush();
            close(bw);
            return true;
        } catch (Exception e) {
            close(bw);
            return false;
        }
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }
}

Related

  1. parseNameDelimiterValueNewLineFile( File nameDelimiterValueNewLineFile, String nameValueDelimiter, Charset charset)
  2. saveFile(File file, String content, String charsetName)
  3. saveToFile(File dest, String contents, Charset cs)
  4. setFileText(File file, Charset charset, String text)
  5. setPropertiesVaule(File file, String key, String value, String comments, Charset charset)
  6. stringToFile(final String s, final File f, final Charset c)
  7. stringToFile(final String string, final Path path, final Charset charset)
  8. toFiles(@Nonnull Process p, @Nonnull Charset charset)
  9. toString(File file, String charset)