Java File to OutputStream openOutputStream(String filePath)

Here you can find the source of openOutputStream(String filePath)

Description

open Output Stream

License

Apache License

Declaration

public static FileOutputStream openOutputStream(String filePath) 

Method Source Code

//package com.java2s;
/*//from  w  ww  .  ja  v  a 2s .  co m
 * Copyright 2002-2012 the original author or authors.
 *
 * 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.File;

import java.io.FileOutputStream;

public class Main {

    public static FileOutputStream openOutputStream(File file) {
        return openOutputStream(file, false);
    }

    public static FileOutputStream openOutputStream(File file, boolean append) {

        if (file.exists()) {

            if (file.isDirectory()) {
                return null;
            }
            if (!file.canWrite()) {
                return null;
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    return null;
                }
            }
        }

        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file, append);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fos;
    }

    public static FileOutputStream openOutputStream(String filePath) {
        return openOutputStream(new File(filePath), false);
    }

    public static FileOutputStream openOutputStream(String filePath, boolean append) {
        return openOutputStream(new File(filePath), append);
    }
}

Related

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