Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Files.walkFileTree doesn't check all the directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Files.walkFileTree doesn Watch "Files.walkFileTree doesn New topic
Author

Files.walkFileTree doesn't check all the directories

Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
Hello,

i have the following code:



my problem is when i start the program it visits only 5 directories with it's subdirectories, but in my /home folder there are much more directories.
What's wrong with my implementation?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

What is your folder structure inside the /home directory? I would like to see that.


~ Mansukh
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Rakoczi Markus wrote:
my problem is when i start the program it visits only 5 directories with it's subdirectories, but in my /home folder there are much more directories.
What's wrong with my implementation?

There must be no files in "home" folder and it's subfolders.
Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
surlac surlacovich wrote:
Rakoczi Markus wrote:
my problem is when i start the program it visits only 5 directories with it's subdirectories, but in my /home folder there are much more directories.
What's wrong with my implementation?

There must be no files in "home" folder and it's subfolders.


Sorry, but what do you mean by "no files in home folder and it's subfolders" ?

@Mansukhdeep Thind

Here is a picture with folder content :


In the source code on my computer of course the path is "/home/myUserName" and the screenshot as well.

only the folders below are visited
.thumbnails
.macromedia
Workspace
.netbeans
.mozilla

in .mozilla folder at one point i get the following ERROR:

java.nio.file.NoSuchFileException: /home/myUserName/.mozilla/firefox/mwad0hks.default/lock
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:143)
at sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:96)
at java.nio.file.Files.readAttributes(Files.java:1675)
at java.nio.file.Files.size(Files.java:2264)
at test_files.Test_Files$1.visitFile(Test_Files.java:36)
at test_files.Test_Files$1.visitFile(Test_Files.java:32)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:135)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:69)
at java.nio.file.Files.walkFileTree(Files.java:2591)
at java.nio.file.Files.walkFileTree(Files.java:2624)
at test_files.Test_Files.main(Test_Files.java:32)



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So an exception is thrown after those five folders are processed, and processing terminates because of that. Which is why the rest of the folders aren't processed.
Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
Thanks for everyone, problem solved!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Paul Clapham wrote:Okay. So an exception is thrown after those five folders are processed, and processing terminates because of that. Which is why the rest of the folders aren't processed.


Why is it that he is unable to access the mozilla folder completely? Has it got something to do with the access Paul?

Rakoczi Markus wrote: Thanks for everyone, problem solved!


Why were you getting an exception when trying to access the directories under mozilla folder? How did you solve it? Was it some access related issue?
Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
There are files in the .mozilla folder where you don't have access or maybe they are temporary in the Cache folder . (Actually if I stop Firefox the program doesn't give error on .mozilla folder).
The solution was to handle the java.nio.file.NoSuchFileException
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Rakoczi Markus wrote:
Sorry, but what do you mean by "no files in home folder and it's subfolders" ?

Your code prints out files and skips folders without any. So if you have structure of folders in "home" directory without any files, it won't print anything, and you will see it as there wasn't any visits.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Rakoczi Markus wrote:There are files in the .mozilla folder where you don't have access or maybe they are temporary in the Cache folder . (Actually if I stop Firefox the program doesn't give error on .mozilla folder).
The solution was to handle the java.nio.file.NoSuchFileException


I was expecting something like this.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Rakoczi Markus wrote:
/home/myUserName/.mozilla/firefox/mwad0hks.default/lock


It happens because Firefox uses lock files. But why java throws exception that file doesn't exists?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Files.walkFileTree doesn't check all the directories