Java File to OutputStream openOutputStream(File file)

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

Description

open Output Stream

License

Open Source License

Declaration

public static FileOutputStream openOutputStream(File file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;

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

public class Main {

    public static FileOutputStream openOutputStream(File file) throws IOException {
        if (file.exists()) {

            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }//  w  w  w . ja v a 2 s  .  com
            if (!file.canWrite()) {
                throw new IOException("File '" + file + "' can not be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' can not be created");
                }
            }
        }
        return new FileOutputStream(file);
    }
}

Related

  1. newOutputStream()
  2. newOutputStream()
  3. newOutputStream(File file)
  4. newOutputStream(File file)
  5. openOutputStream(File file)
  6. openOutputStream(File file)
  7. openOutputStream(File file)
  8. openOutputStream(File file, boolean append, boolean gzip)
  9. openOutputStream(File file, int bufferSize)