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

java.io.File demonstrates incorrect behavior

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • x86
    • linux



      Name: asR10013 Date: 05/16/2000



          In the following sample code java.io.File object is created for
      nonexistent directory "test.java/". The test output shows that the created
      object represent a file and a directory simultaneously (both isFile()
      and isDirectory return true). The FileInputStream created with this
      File object reads content of file test.java whereas
      FileInputStream("test.java/") throws java.io.FileNotFoundException
      (see output below).

          Under Solaris jdk1.3.0Z the same behavior is observed, except
      FileInputStream("test.java/") does not throws java.io.FileNotFoundException
      but creates nonfunctional object instead.

      Note: the source file should be named 'test.java'
      -------------------------------- test.java----------------------------
      import java.io.*;

      class test {
          public static void main(String args[]) throws Exception {
      String filename = "test.java/";
      int b;

      File f = new File(filename);
      System.out.println("f.exists() "+f.exists());
              System.out.println("f.isFile() "+f.isFile());
      System.out.println("f.isDirectory() "+f.isFile());
              System.out.println("f.canRead() "+f.canRead());

      FileInputStream fis1 = new FileInputStream(f);
      while ((b=fis1.read())>0) System.out.write(b);

      FileInputStream fis2 = new FileInputStream(filename);
      while ((b=fis1.read())>0) System.out.write(b);
          }
      }
      -------------------------------- output ------------------------------
      $ java -version
      java version "1.3.0beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0beta-b03)
      Java HotSpot(TM) Client VM (build 1.3.0beta-b02, mixed mode)
      $ java test
      f.exists() true
      f.isFile() true
      f.isDirectory() true
      f.canRead() true
      import java.io.*;

      class test {
          public static void main(String args[]) throws Exception {
      String filename = "test.java/";
      int b;

      File f = new File(filename);
      System.out.println("f.exists() "+f.exists());
              System.out.println("f.isFile() "+f.isFile());
      System.out.println("f.isDirectory() "+f.isFile());
              System.out.println("f.canRead() "+f.canRead());

      FileInputStream fis1 = new FileInputStream(f);
      while ((b=fis1.read())>0) System.out.write(b);

      FileInputStream fis2 = new FileInputStream(filename);
      while ((b=fis1.read())>0) System.out.write(b);
          }
      }

      Exception in thread "main" java.io.FileNotFoundException: test.java/ (Not a directory)
              at java.io.FileInputStream.open(Native Method)
              at java.io.FileInputStream.<init>(FileInputStream.java:64)
              at test.main(test.java:17) while ((b=fis1.read())>0) System.out.write(b);
      }
      ====================================================================== }

      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            alssunw Als Als (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: