Java FileOutputStream Create getOutputStream(final File file)

Here you can find the source of getOutputStream(final File file)

Description

Gets the output stream from a File object.

License

Apache License

Parameter

Parameter Description
file the file.

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Return

the output stream.

Declaration

public static OutputStream getOutputStream(final File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w  . j a  v  a  2s .c o  m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedOutputStream;

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    /**
     * Gets the output stream from a File object.
     *
     * @param file
     *            the file.
     * @return the output stream.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static OutputStream getOutputStream(final File file) throws IOException {
        return getOutputStream(file, false);
    }

    /**
     * Gets the output stream from a File object.
     *
     * @param file
     *            the file
     * @param createFile
     *            If true and the file does not exist it will be create a new file.
     * @return the output stream
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static OutputStream getOutputStream(final File file, final boolean createFile) throws IOException {
        OutputStream os = null;
        BufferedOutputStream bos = null;
        if (file.exists()) {
            os = new FileOutputStream(file);
        } else {
            if (createFile) {
                file.createNewFile();
                os = new FileOutputStream(file);
            } else {
                throw new FileNotFoundException("File " + file.getName() + " does not exist.");
            }
        }
        bos = new BufferedOutputStream(os);
        return bos;
    }
}

Related

  1. getOutputStream(File file)
  2. getOutputStream(File file)
  3. getOutputStream(File file, String filePath)
  4. getOutputStream(final File file)
  5. getOutputStream(final Object obj, final boolean append)
  6. getOutputStream(String file)
  7. getOutputStream(String filename)