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

java.io.File.list does not work when slash appended to drive (win95)

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1.6
    • CPU:
      x86
    • OS:
      windows_95
    • Verification:
      Verified

      Backports

        Description



        Name: rlT66838 Date: 07/16/97


        Create an instance of a File:

        File file = new File("C:\\"); // slash
        String[] contents = file.list();

        contents is null even if C:\ is not empty.

        Try again:
        File file = new File("C:"); // no slash
        String[] contents = file.list();

        now we get something!

        Note:
        works fine on NT with or without the trailing slash.

        ======================================================================
        roger.lewis@Eng 1997-07-22

        If a File object is created on a drive letter (e.g. "c:") without a trailing slash, getAbsolutePath( ) assumes that c: is a file in the current directory.
        Note that the File object itself is correct, as it give a correct directory listing. When a trailing slash is used, everything works correctly, except the
        directory listing (as documented in bug report 4065189).

        import java.io.*;

        public class DirTest {

           public static void main( String argv[] ) {

              File file = new File( "c:" );
              System.err.println( "file.getAbsolutePath( \"c:\" )=" + file.getAbsolutePath( ) );

           }
        }

        C:\Alex>java DirTest
        file.getAbsolutePath( "c:" )=C:\Alex\c:

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

        max.spivak@Eng 1997-08-01

        Here's is some additional cannon fodder. If I'm in c:\mspivak and
        call getParent(), I get c:\. If I then do a list() on that, it will
        fail on Win95. Yes, a workaround exists to not use the \, but
        so much for platform independence and WORE!

        The code is followed by the output:

        import java.io.*;
        public class dirlist {
            public static void main(String[] args) {
        if (args.length != 1) {
        System.out.println("usage: java dirlist dirname");
        return;
        }

        System.out.println("testing directory: " + args[0]);
        File dir = new File(args[0]);

        if (dir.exists())
        System.out.println(dir + " exists");
        else
        System.out.println(dir + " doest NOT exist");

        if (dir.isDirectory())
        System.out.println(dir + " is a directory");
        else
        System.out.println(dir + " is NOT directory");

        if (dir.isFile())
        System.out.println(dir + " is a file");
        else
        System.out.println(dir + " is NOT a file");

        System.out.println("path = " + dir.getPath());
        System.out.println("abs path = " + dir.getAbsolutePath());
        try { System.out.println("can path = " + dir.getCanonicalPath());}
        catch (IOException ex) {ex.printStackTrace();}
        System.out.println("name = " + dir.getName());
        System.out.println("parent = " + dir.getParent());

        System.out.println("can read = " + dir.canRead());
        System.out.println("can write = " + dir.canWrite());

        String files[] = dir.list();
        System.out.println("files = " + files);
        System.out.println("length = " + files.length);

            }
        }

        Now the output:

        F:\tmp>java dirlist c:
        testing directory: c:
        c: exists
        c: is NOT directory
        c: is NOT a file
        path = c:
        abs path = F:\tmp\c:
        can path = F:\tmp\c:
        name = c:
        parent = null
        can read = true
        can write = true
        files = [Ljava.lang.String;@1cc81a
        length = 31

        F:\tmp>java dirlist c:testing directory: c:c:\ exists
        c:\ is a directory
        c:\ is NOT a file
        path = c:abs path = c:can path = c:name =
        parent = null
        can read = true
        can write = true
        files = null
        java.lang.NullPointerException:
                at dirlist.main(dirlist.java:40)

        F:\tmp>java dirlist c:\mspivak
        testing directory: c:\mspivak
        c:\mspivak exists
        c:\mspivak is a directory
        c:\mspivak is NOT a file
        path = c:\mspivak
        abs path = c:\mspivak
        can path = c:\mspivak
        name = mspivak
        parent = c:can read = true
        can write = true
        files = [Ljava.lang.String;@1cc7bb
        length = 3

        F:\tmp>java dirlist c:\mspivaktesting directory: c:\mspivakc:\mspivak\ exists
        c:\mspivak\ is a directory
        c:\mspivak\ is NOT a file
        path = c:\mspivakabs path = c:\mspivakcan path = c:\mspivak
        name =
        parent = c:\mspivak
        can read = true
        can write = true
        files = [Ljava.lang.String;@1cc7bb
        length = 3

        Running this on WinNT gives the following output:

        F:\tmp>java dirlist c:
        testing directory: c:
        c: exists
        c: is NOT directory
        c: is NOT a file
        path = c:
        abs path = F:\tmp\c:
        can path = F:\tmp\c:
        name = c:
        parent = null
        can read = true
        can write = true
        files = [Ljava.lang.String;@1ea81a
        length = 31

        F:\tmp>java dirlist c:testing directory: c:c:\ exists
        c:\ is a directory
        c:\ is NOT a file
        path = c:abs path = c:can path = c:name =
        parent = null
        can read = true
        can write = true
        files = [Ljava.lang.String;@1ea813
        length = 31

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

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                mr Mark Reinhold
                Reporter:
                rlewis Roger Lewis (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: