Java PrintWriter Create getPrintWriter(File file)

Here you can find the source of getPrintWriter(File file)

Description

Init.

License

LGPL

Parameter

Parameter Description
file -- the output file

Exception

Parameter Description
IOException an exception

Return

PrintWriter for file

Declaration

public static PrintWriter getPrintWriter(File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

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

import java.util.zip.GZIPOutputStream;

public class Main {
    /**// w  w w . ja  v  a 2s  . c  o  m
     * Init. a writer for the given file object.
     * In the case of .gz file name ending a GZIP compressed writer is returned.
     * @param file -- the output file
     * @return PrintWriter for file
     * @throws IOException
     */
    public static PrintWriter getPrintWriter(File file) throws IOException {
        //check for compressing
        if (file.getName().endsWith(".gz")) {
            return new PrintWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(file))));
        }

        return new PrintWriter(file);
    }
}

Related

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