Java File to OutputStream openOutputStream(File file, int bufferSize)

Here you can find the source of openOutputStream(File file, int bufferSize)

Description

Opens an OutputStream on a file for writing.

License

Open Source License

Parameter

Parameter Description
file the file to open a stream on
bufferSize if this is > 0, use it as the buffer size for the stream

Exception

Parameter Description
NullPointerException if file is null
IllegalArgumentException if bufferSize < 0

Return

an OutputStream on the file, or null if an exception was thrown

Declaration

public static OutputStream openOutputStream(File file, int bufferSize) 

Method Source Code

//package com.java2s;
/*//from w w w .  j  av a2 s . c om
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.BufferedOutputStream;

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    private static final String ex_null_param = "A required paramater is null";

    /**
     * Opens an OutputStream on a file for writing.
     *
     * If the file exists and has content, it will be overwritten. That is to say the append
     * paramater will be false.
     * 
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileOutputStream constructor would.
     *
     * @param file the file to open a stream on
     * @return an OutputStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     */
    public static OutputStream openOutputStream(File file) {
        return openOutputStream(file, false, 0);
    }

    /**
     * Opens an OutputStream on a file for writing.
     *
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileOutputStream constructor would.
     *
     * @param file the file to open a stream on
     * @param append if true, open the stream in append mode
     * @return an OutputStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     */
    public static OutputStream openOutputstream(File file, boolean append) {
        return openOutputStream(file, append, 0);
    }

    /**
     * Opens an OutputStream on a file for writing.
     *
     * The stream will be wrapped in a buffered stream if bufferSize is > 0.
     *
     * If the file exists and has content, it will be overwritten. That is to say the append
     * paramater will be false.
     * 
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileOutputStream constructor would.
     *
     * @param file the file to open a stream on
     * @param bufferSize if this is > 0, use it as the buffer size for the stream
     * @return an OutputStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     * @throws IllegalArgumentException if bufferSize < 0
     */
    public static OutputStream openOutputStream(File file, int bufferSize) {
        return openOutputStream(file, false, bufferSize);
    }

    /**
     * Opens an OutputStream on a file for writing.
     *
     * The stream will be wrapped in a buffered stream if bufferSize is > 0.
     * 
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileOutputStream constructor would.
     *
     * @param file the file to open a stream on
     * @param bufferSize if this is > 0, use it as the buffer size for the stream
     * @param append if true, open the stream in append mode
     * @return an OutputStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     * @throws IllegalArgumentException if bufferSize < 0
     */
    public static OutputStream openOutputStream(File file, boolean append, int bufferSize) {
        checkNull(file);

        try {
            OutputStream out = new FileOutputStream(file, append);

            if (bufferSize > 0) {
                out = new BufferedOutputStream(out, bufferSize);
            }
            return out;
        } catch (FileNotFoundException e) {
            return null;
        } catch (SecurityException e2) {
            return null;
        }
    }

    /**
     * Check for null objects in a list of objects.
     */
    private static void checkNull(Object... objs) {
        for (Object obj : objs) {
            if (obj == null)
                throw new NullPointerException(ex_null_param);
        }
    }
}

Related

  1. openOutputStream(File file)
  2. openOutputStream(File file)
  3. openOutputStream(File file)
  4. openOutputStream(File file)
  5. openOutputStream(File file, boolean append, boolean gzip)
  6. openOutputStream(String filePath)
  7. openOutputStream(String outputName)
  8. outStream(final File aFile)
  9. outStream(String file)