8 Replies Latest reply: Mar 10, 2013 8:01 AM by 901555 RSS

    enhanced for loop with DirectoryStream  in java 7

    901555
      Hi, I'm a newbie to Java and am having trouble with the following code, which I'm trying to use to iterate through a directory and obtain the file names of all the pics to create thumbnails of them.

      I've done hours and hours of searching on the web and find no answer for this one, so it's probably something very simple I've screwed up.

      All assitance is appreciated.

      The code segment:
              try (DirectoryStream<Path> ds = 
                Files.newDirectoryStream(FileSystems.getDefault().getPath(dirNew)))
              {
                    System.out.println("at dirstream");
                    System.out.format("toString: %s%n", ds.toString());
                  for (Path p : ds) 
                  {
                      // Iterate over the paths in the directory and print filenames
                     System.out.println(p.getFileName()); 
                     System.out.println("at Path p : ds");
                      thumbName = p.getFileName();
                      createThumb(String.format("%s",thumbName)); 
                  }
            
              } catch (IOException e) {
                  System.out.println("dirstream failed!!!");
      //           e.printStackTrace();
              }
                  System.out.println("atend of dirstream");
      and the output:

      run:
      /home/rich/Images/FromWeb-Misc/1
      at dirstream
      toString: sun.nio.fs.UnixSecureDirectoryStream@196b20e
      atend of dirstream

      Edited by: 898552 on Mar 6, 2013 3:04 PM

      Edited by: 898552 on Mar 6, 2013 3:07 PM

      Edited by: 898552 on Mar 6, 2013 3:09 PM
        • 1. Re: enhanced for loop with DirectoryStream  in java 7
          EJP
          What's the expected output? What's the exception? if any? What's the question?
          • 2. Re: enhanced for loop with DirectoryStream  in java 7
            901555
            The expected output of this code segment is just the filenames of the files in the directory.

            The question is why does the for loop not execute (which it obviously does not from the debugging print statements and the output.

            Thanks

            Edited by: 898552 on Mar 6, 2013 3:33 PM
            • 3. Re: enhanced for loop with DirectoryStream  in java 7
              901555
              The expected output is the filenames of the files in the directory.

              The question is : Why does the for loop not execute? (it obvously does not from the debugging print statments and the output).

              Thanks
              • 4. Re: enhanced for loop with DirectoryStream  in java 7
                rp0428
                Download the free NetBeans IDE, debug your code and step through it.
                http://netbeans.org/downloads/

                Set a breakpoint inside the 'for' loop and when you get there take a peek at your variables and see what they contain.

                Use the 'Debug - Evaluate Expression' window and print out those variables to see what you get.

                You learn by doing. You can answer this question yourself using the debugger that is part of NetBeans.
                • 5. Re: enhanced for loop with DirectoryStream  in java 7
                  Kayaman
                  rp0428 wrote:
                  Set a breakpoint inside the 'for' loop and when you get there take a peek at your variables and see what they contain.
                  It's not entering the loop.

                  Now, for the obvious questions: are there any files in the directory? Does it work with the regular for loop?
                  • 6. Re: enhanced for loop with DirectoryStream  in java 7
                    rp0428
                    >
                    It's not entering the loop.
                    >
                    With the versions of NetBeans I use you can set breakpoints BEFORE and even ON the loop! Being able to set them inside a loop is just a no-cost extra. ;)

                    Using a GUI with a debugger is the most efficient way for OP to start learning how to troubleshoot their own problems.

                    Relax - take a deep breath - repeat after me
                    >
                    5 more points - 5 more points - 5 more points
                    >
                    Congratulations - I see a 'shiny' future for you!

                    Status Level: Expert (2,495) :D
                    • 7. Re: enhanced for loop with DirectoryStream  in java 7
                      Kayaman
                      rp0428 wrote:
                      >
                      It's not entering the loop.
                      >
                      With the versions of NetBeans I use you can set breakpoints BEFORE and even ON the loop! Being able to set them inside a loop is just a no-cost extra. ;)
                      I know! But you said "IN THE LOOP" :D The poor guy will set his breakpoint in it and think his debugger is broken ;)
                      Relax - take a deep breath - repeat after me
                      >
                      5 more points - 5 more points - 5 more points
                      >
                      Congratulations - I see a 'shiny' future for you!

                      Status Level: Expert (2,495) :D
                      Haha, I hadn't noticed that :D I better be extra helpful now, so I can pop open a bottle of champagne tonight ;)
                      • 8. Re: enhanced for loop with DirectoryStream  in java 7
                        901555
                        Thanks you all for your replies, I have always used NetBeans, just not the debugger so much, although in this case, it didn't solve the problem, except to point out to me the content of the variable I used for the directory name.

                        As I suspected, it was a dumb error on my part.

                        I had just created the directory contained in dirNew, from a base in dirPath.

                        I needed to use dirPath in my DirectoryStream statement.

                        It works fine now.

                        Again thanks for the help.