Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4007385

java.io.FileOutputStream.close() method does not work properly

XMLWordPrintable

    • sparc
    • solaris_2.5

      This bug was found by St.Petersburg Java SQE team (by Alexander Kuzmin).

      The java.io.FileOutputStream.close() does not work properly with IOException.
      If the file that FileOutputStream object connected to does not exist already -
      FileOutputStream.close does not throw IOException.


      The Java Language Specification Version 1.0:

      22.16.8 public void close() throws IOException

      This file output stream is closed and may no longer be used for writing bytes.

      Overrides the close method of OutputStream (§22.15.5).

      _______________________Example ______________________________________
      import java.io.*;
      public class badtests {
        public static void main( String argv[] ) {
         File f=null;
         FileOutputStream testfile=null;
       
          try {
           f=new File("to_delete");
          } catch (Throwable e )
             {System.out.println("Unable to create file.");
              return;
             }

          try {
           testfile=new FileOutputStream(f);
          }
           catch (SecurityException e) //step Catch Exceptions
           {System.out.println("Unexpected SecurityException !");}
           catch (Exception e) //step Catch Exceptions
           {System.out.println(" Unexpected "+e+" thrown ");}

          try {
          testfile.write(100);// write something
          } catch (Throwable e ) {System.out.println("Enable to write !");}

          try {
           f.delete(); // Delete our file
          } catch (Throwable e) {System.out.println("Unable to delete file.");
              return; }

          try {
          testfile.close(); // Our file does not exist
          } catch (IOException e)
            {System.out.println("OK ! IOException"); //will newer
              return;
            }
            catch (Throwable e )
             {System.out.println("IOExeption not thrown ! But "+e);
              return;
             }

          System.out.println("IOException not thrown !");
        }
      }
        
      _______________________ Result _____________________________________
      IOException not thrown !
      ____________________________________________________________________

            Unassigned Unassigned
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: