Java FileOutputStream Create getOutputStream(final Object obj, final boolean append)

Here you can find the source of getOutputStream(final Object obj, final boolean append)

Description

Turn an object into an OutputStream if possible.

License

Open Source License

Parameter

Parameter Description
obj an OutputStream or File.
append if obj is a file and this parameter is true, the output stream will be opened in append mode.

Exception

Parameter Description
IOException if an error occurs

Return

an OutputStream. If obj is a File, the stream is Buffered.

Declaration

public static OutputStream getOutputStream(final Object obj, final boolean append) throws IOException 

Method Source Code

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

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**//w  w w  .  j  av a2s . com
     * Turn an object into an OutputStream if possible.
     *
     * @param obj
     *            an OutputStream or File.
     * @param append
     *            if obj is a file and this parameter is true, the output stream
     *            will be opened in append mode.
     * @return an OutputStream. If obj is a File, the stream is Buffered.
     * @throws IOException
     *             if an error occurs
     */
    public static OutputStream getOutputStream(final Object obj, final boolean append) throws IOException {
        OutputStream stream = null;

        if (obj instanceof OutputStream) {
            stream = (OutputStream) obj;
        } else if (obj instanceof File) {
            File file = (File) obj;
            // create parent directory first, if needed
            File parent = file.getAbsoluteFile().getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Unable to create directory " + parent.getAbsolutePath());
                }
            }
            if (!parent.canWrite()) {
                throw new IOException("Do not have write permission for directory " + parent.getAbsolutePath());
            }

            stream = new BufferedOutputStream(new FileOutputStream(file, append));
        } else {
            throw new IllegalArgumentException("Expected an OutputStream or File");
        }

        return stream;
    }

    /**
     * Same as calling getOutputStream(obj, false). If obj is a file, it will
     * open a new output stream and will not append.
     *
     * @param obj
     *            an OutputStream or File.
     * @return an OutputStream. If obj is a File, the stream is Buffered.
     * @throws IOException
     *             if an error occurs.
     */
    public static OutputStream getOutputStream(final Object obj) throws IOException {
        return getOutputStream(obj, false);
    }
}

Related

  1. getOutputStream(File file)
  2. getOutputStream(File file)
  3. getOutputStream(File file, String filePath)
  4. getOutputStream(final File file)
  5. getOutputStream(final File file)
  6. getOutputStream(String file)
  7. getOutputStream(String filename)
  8. getOutputStream(String fileName)
  9. getOutputStream(String filename, Map map)