Java Text File Write by Charset writer(String path, String content, String charset)

Here you can find the source of writer(String path, String content, String charset)

Description

writer

License

Open Source License

Declaration

public static void writer(String path, String content, String charset) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

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

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.Channel;

public class Main {
    public static void writer(String path, byte[] b) throws IOException {
        writer(path, b, false);//from www .  j  ava2s.  c om
    }

    public static void writer(String path, byte[] b, boolean isAppend) throws IOException {
        OutputStream out = fileOut(path, isAppend);
        out.write(b);
        close(out);
    }

    public static void writer(String path, String content) throws IOException {
        writer(path, content, "UTF-8");
    }

    public static void writer(String path, String content, String charset) throws IOException {
        OutputStream out = fileOut(path, false);
        out.write(content.getBytes(charset));
        close(out);
    }

    public static OutputStream fileOut(String path, boolean append) throws FileNotFoundException {
        return fileOut(new File(path), append);
    }

    public static OutputStream fileOut(File file, boolean append) throws FileNotFoundException {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new FileOutputStream(file, append);
    }

    public static void close(InputStream in) {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

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

    public static void close(OutputStream out) {
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Reader reader) {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Writer writer) {
        if (null != writer) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Channel channel) {
        if (null != channel) {
            if (channel.isOpen()) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. writeFile(String content, File file, Charset encoding)
  2. writeFile(String path, String content, Charset charset)
  3. writeFile(String path, String content, Charset encoding)
  4. writeFile(String path, String output, Charset encoding)
  5. writer(OutputStream out, String charset)
  6. writerBuffered(final File file, final Charset charset)
  7. writeStreamToString(InputStream is, String charsetName)
  8. writeString(OutputStream os, String string, CharsetEncoder encoder)
  9. writeString(OutputStream out, String charset, String value)