Java Text File Write by Charset write(char[] data, File file, String charsetName)

Here you can find the source of write(char[] data, File file, String charsetName)

Description

write

License

Apache License

Declaration

public static void write(char[] data, File file, String charsetName) 

Method Source Code

//package com.java2s;
/**/*from   www .ja  v  a  2 s  .c om*/
 * Copyright (c) 2014-2015, biezhi ?? (biezhi.me@gmail.com)
 *
 * 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.
 */

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLConnection;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.util.zip.ZipFile;

public class Main {
    public static void write(byte[] data, File file) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(data);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            closeQuietly(os);
        }
    }

    public static void write(char[] data, File file, String charsetName) {
        write(data, file, Charset.forName(charsetName));
    }

    public static void write(char[] data, File file, Charset charset) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(new String(data).getBytes(charset));
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            closeQuietly(os);
        }
    }

    public static void write(String data, File file, String charsetName) {
        write(data, file, Charset.forName(charsetName));
    }

    public static void write(String data, File file, Charset charset) {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(data.getBytes(charset));
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            closeQuietly(os);
        }
    }

    public static void closeQuietly(ZipFile obj) {
        try {
            if (obj != null) {
                obj.close();
            }
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(Socket socket) {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(ServerSocket socket) {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(Selector selector) {
        try {
            if (selector != null) {
                selector.close();
            }
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(URLConnection conn) {
        if (conn != null) {
            if (conn instanceof HttpURLConnection) {
                ((HttpURLConnection) conn).disconnect();
            }
        }
    }

    public static void closeQuietly(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. newWriter(File file, Charset charset)
  2. newWriter(OutputStream output, Charset encoding)
  3. newWriter(WritableByteChannel ch, Charset cs)
  4. openTextFileForWriting(File f, Charset encoding)
  5. openWriter(OutputStream stream, Charset charset, boolean autoflush)
  6. write(File file, Charset charset, String content)
  7. write(File file, Object content, Charset cs, boolean append)
  8. write(final String s, final OutputStream out, Charset charset)
  9. write(Path file, CharBuffer data, Charset cs)