3 Replies Latest reply: Jun 2, 2009 11:57 AM by 807588 RSS

    Directory Contents

    807588
      Hi,

      I would like to list a directory contents. I got this code from a website:

      Path dir = ...;
      DirectoryStream<Path> stream = null;
      try {
      stream = dir.newDirectoryStream();
      for (Path file: stream) {
      System.out.println(file.getName());
      }
      } catch (IOException x) {
      //IOException can never be thrown by the iteration.
      //In this snippet, it can only be thrown by newDirectoryStream.
      System.err.println(x);
      } finally {
      if (stream != null) stream.close();
      }

      When I put it in NetBeans it doesn't find the class Path.
      Where is it defined?
      Thanks in advance,

      Luis Vital
        • 1. Re: Directory Contents
          793415
          Welcome to the Sun forums.
          >
          ...
          I would like to list a directory contents. I got this code from a website:>
          The web is a big place. Care to narrow that down with an URL to this website?
          >
          Path dir = ...;
          DirectoryStream<Path> stream = null;
          try {
          stream = dir.newDirectoryStream();
          for (Path file: stream) {
          System.out.println(file.getName());
          }
          } catch (IOException x) {
          //IOException can never be thrown by the iteration.
          //In this snippet, it can only be thrown by newDirectoryStream.
          System.err.println(x);
          } finally {
          if (stream != null) stream.close();
          }>
          When posting code, or code snippets, please use the code formatting tags. To do that, select the code and click the CODE button seen on the Plain Text tab of the message posting form.
          >
          When I put it in NetBeans it doesn't find the class Path.
          Where is it defined?>
          You might try looking further around that 'website' you mentioned. But short of you supplying an URL, how on earth would you expect us to know?
          • 2. Re: Directory Contents
            807588
            Hi,

            What I would like to do is to list the contents of a directory in my hard disk.
            I appologise for some mistakes I could have done but I am new in posting in this site.
            Thanks,

            Luis Vital
            • 3. Re: Directory Contents
              807588
              [File.listFiles()|http://java.sun.com/javase/6/docs/api/java/io/File.html#listFiles()]