Java PrintWriter Create getPrintwriter(File file, boolean append)

Here you can find the source of getPrintwriter(File file, boolean append)

Description

Return print writer for file with UTF8 encoding.

License

Open Source License

Parameter

Parameter Description
file - file to write into
append - append or overwrite file content

Declaration

public static PrintWriter getPrintwriter(File file, boolean append) throws IOException 

Method Source Code

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

import java.io.File;

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

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**/*from  w  w w .j  a va2s  . c om*/
     * Return print writer for file with UTF8 encoding.
     * <p>
     * If filename ends with .gz - file will be compressed 
     * 
     * @param file - file to write into
     * @param append - append or overwrite file content
     * */
    public static PrintWriter getPrintwriter(File file, boolean append) throws IOException {

        OutputStream os = new FileOutputStream(file, append);
        if (file.getName().endsWith(".gz")) {
            os = new GZIPOutputStream(os);
        }

        return new PrintWriter(new OutputStreamWriter(os, "UTF8"));
    }
}

Related

  1. getPrintWriter(File f)
  2. getPrintWriter(File file)
  3. getPrintWriter(File file)
  4. getPrintWriter(final File aFile, final boolean append)
  5. getPrintWriter(String filename)
  6. getPrintWriter(String fileName, Writer out)
  7. getPrintWriter(String nombreFichero)