Java PrintStream Create getPrintStream(final OutputStream out)

Here you can find the source of getPrintStream(final OutputStream out)

Description

Retrieves a PrintStream for the given OutputStream

License

Open Source License

Parameter

Parameter Description
out the OutputStream

Return

the PrintStream

Declaration

public static final PrintStream getPrintStream(final OutputStream out) 

Method Source Code

//package com.java2s;
/**/*from  w  w w.ja va 2s  .c  o  m*/
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import java.io.*;

public class Main {
    private final static boolean DEF_APPEND = false;

    /**
     * Retrieves a PrintStream for the given OutputStream
     * 
     * @param out the OutputStream
     * @return the PrintStream
     **/
    public static final PrintStream getPrintStream(final OutputStream out) {
        return out instanceof PrintStream ? (PrintStream) out : new PrintStream(out);
    }

    public static final PrintStream getPrintStream(final String loc) {
        try {
            return getPrintStream(getFileOutputStream(loc));
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Retrieves a OutputStream for the given absolute path, creating any necessary directories
     * 
     * @param absolutePath the absolute path
     * @return the OutputStream
     * @throws IOException if an I/O problem occurs
     **/
    public final static OutputStream getFileOutputStream(final String absolutePath) throws IOException {
        return getFileOutputStream(absolutePath, DEF_APPEND);
    }

    /**
     * Retrieves a OutputStream for the given absolute path, creating any necessary directories
     * 
     * @param absolutePath the absolute path
     * @param append whether the file should be opened in append mode
     * @return the OutputStream
     * @throws IOException if an I/O problem occurs
     **/
    public final static OutputStream getFileOutputStream(final String absolutePath, final boolean append)
            throws IOException {
        return getFileOutputStream(new File(absolutePath), append);
    }

    public final static OutputStream getFileOutputStream(final File f) throws IOException {
        return getFileOutputStream(f, DEF_APPEND);
    }

    public final static OutputStream getFileOutputStream(final File f, final boolean append) throws IOException {
        final File dir = f.getParentFile();

        if ((dir != null) && !dir.exists()) {
            dir.mkdirs();
        }

        return new FileOutputStream(f, append);
    }

    /**
     * Retrieves whether the file with the given name exists
     * 
     * @param fileName the file name
     * @return whether the file exists
     **/
    public final static boolean exists(final String fileName) {
        return fileName == null ? false : new File(fileName).exists();
    }
}

Related

  1. getPrintStream(String file)
  2. getPrintStream(String file)
  3. getPrintStream(String fileName)
  4. getPrintStream(String filename, boolean append)