src/share/classes/java/io/FileOutputStream.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 178      *                   rather than a regular file, does not exist but cannot
 179      *                   be created, or cannot be opened for any other reason
 180      * @exception  SecurityException  if a security manager exists and its
 181      *               <code>checkWrite</code> method denies write access
 182      *               to the file.
 183      * @see        java.io.File#getPath()
 184      * @see        java.lang.SecurityException
 185      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
 186      * @since 1.4
 187      */
 188     public FileOutputStream(File file, boolean append)
 189         throws FileNotFoundException
 190     {
 191         String name = (file != null ? file.getPath() : null);
 192         SecurityManager security = System.getSecurityManager();
 193         if (security != null) {
 194             security.checkWrite(name);
 195         }
 196         if (name == null) {
 197             throw new NullPointerException();



 198         }
 199         this.fd = new FileDescriptor();
 200         fd.attach(this);
 201         this.append = append;
 202 
 203         open(name, append);
 204     }
 205 
 206     /**
 207      * Creates a file output stream to write to the specified file
 208      * descriptor, which represents an existing connection to an actual
 209      * file in the file system.
 210      * <p>
 211      * First, if there is a security manager, its <code>checkWrite</code>
 212      * method is called with the file descriptor <code>fdObj</code>
 213      * argument as its argument.
 214      * <p>
 215      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 216      * is thrown.
 217      * <p>


   1 /*
   2  * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 178      *                   rather than a regular file, does not exist but cannot
 179      *                   be created, or cannot be opened for any other reason
 180      * @exception  SecurityException  if a security manager exists and its
 181      *               <code>checkWrite</code> method denies write access
 182      *               to the file.
 183      * @see        java.io.File#getPath()
 184      * @see        java.lang.SecurityException
 185      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
 186      * @since 1.4
 187      */
 188     public FileOutputStream(File file, boolean append)
 189         throws FileNotFoundException
 190     {
 191         String name = (file != null ? file.getPath() : null);
 192         SecurityManager security = System.getSecurityManager();
 193         if (security != null) {
 194             security.checkWrite(name);
 195         }
 196         if (name == null) {
 197             throw new NullPointerException();
 198         }
 199         if (file.isInvalid()) {
 200             throw new FileNotFoundException("Invalid file path");
 201         }
 202         this.fd = new FileDescriptor();
 203         fd.attach(this);
 204         this.append = append;
 205 
 206         open(name, append);
 207     }
 208 
 209     /**
 210      * Creates a file output stream to write to the specified file
 211      * descriptor, which represents an existing connection to an actual
 212      * file in the file system.
 213      * <p>
 214      * First, if there is a security manager, its <code>checkWrite</code>
 215      * method is called with the file descriptor <code>fdObj</code>
 216      * argument as its argument.
 217      * <p>
 218      * If <code>fdObj</code> is null then a <code>NullPointerException</code>
 219      * is thrown.
 220      * <p>