Java FileOutputStream Create getOutputStream(String root, String name, boolean overwrite, boolean verbose)

Here you can find the source of getOutputStream(String root, String name, boolean overwrite, boolean verbose)

Description

get Output Stream

License

Open Source License

Declaration

private static OutputStream getOutputStream(String root, String name,
            boolean overwrite, boolean verbose) throws IOException 

Method Source Code

//package com.java2s;
/**//  w  ww . ja  va  2 s  .c  om
 * Copyright (c) 2003,2004 International Business Machines Corporation.
 * All Rights Reserved.
 *
 * This software is provided and licensed under the terms and conditions
 * of the Common Public License:
 * http://oss.software.ibm.com/developerworks/opensource/license-cpl.html
 */

import java.io.*;

public class Main {
    private static OutputStream getOutputStream(String root, String name,
            boolean overwrite, boolean verbose) throws IOException {
        if (root != null) {
            File directory = new File(root);

            if (!directory.exists()) {
                if (!directory.mkdirs()) {
                    throw new IOException("Failed to create directory '"
                            + root + "'.");
                } else if (verbose) {
                    System.out.println("Created directory '"
                            + directory.getAbsolutePath() + "'.");
                }
            }
        }

        File file = new File(root, name);
        String absolutePath = file.getAbsolutePath();

        if (file.exists()) {
            if (!overwrite) {
                throw new IOException("File '" + absolutePath
                        + "' already exists. "
                        + "Please remove it or enable the "
                        + "overwrite option.");
            } else {
                file.delete();

                if (verbose) {
                    System.out.println("Deleted file '" + absolutePath
                            + "'.");
                }
            }
        }

        FileOutputStream fos = new FileOutputStream(absolutePath);

        if (verbose) {
            System.out.println("Created file '" + absolutePath + "'.");
        }

        return fos;
    }
}

Related

  1. getOutputStream(String filename, Map map)
  2. getOutputStream(String filename, String dir)
  3. getOutputStream(String fname, String enc, boolean append)
  4. getOutputStream(String path)
  5. getOutputStream(String path)
  6. getOutputStream(String theFilePath)
  7. getOutputStreamForFile(final File file)
  8. getOutputStreamForFile(String filename)
  9. writeFile(File outFile, ZipInputStream zipInputStream, ZipEntry entry)