• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Traversal  RSS feed

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need to traverse through all the files in a system. If i use walkFileTree and override the methods, it traverses only to a particular set of folders,even if i set the path to root folder("/"). If i use listRoots and listFiles and make a loop. I can traverse through only one folder in the root. Other folders are ignored. Please do not ask me to give the code, because I am not in a situation to reveal the code. Sorry for that. Can someone please help me with this?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Hi,

I need to traverse through all the files in a system. If i use walkFileTree and override the methods, it traverses only to a particular set of folders,even if i set the path to root folder("/"). If i use listRoots and listFiles and make a loop. I can traverse through only one folder in the root. Other folders are ignored. Please do not ask me to give the code, because I am not in a situation to reveal the code. Sorry for that. Can someone please help me with this?


Surely you just need to use your walkFileTree() code on each root obtained from listRoots() .

I don't understand "I am not in a situation to reveal the code" since I don't see how there can be anything proprietary in simple file tree walking code.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Richard,

Thanks for answering. My problem is if I use walkFileTree I am not able to access all the files inside a folder. I am working in Ubuntu environment,so I have only one root. When I use walkFileTree, it searches only some folders, but I need to access the files and folders in the entire system. I did not use listRoots with walkFileTree. I tried to use listRoots and listFiles to get my own working code for file traversal,but it did not work. I ended up in traversing only the first folder in the root. If the folder I traversed has subfolders only the first subfolder is traversed,others are ignored and even other folders in the root other than the first folder is ignored.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I primarily use Ubuntu and have several Java programs that traverse parts of the file system and these work without problems. I suspect you are not running your code as root so you won't have permission to access directories directly beneath "/" . Solution - run your application as root using 'sudo' .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:I am working in Ubuntu environment,so I have only one root.

I fear you and Richard are using the word "root" differently. There is a root user and a "directory root". I have never used walkFileTree, but I would expect that if it follows the general recursive pattern of trees, each directory is "its own root" - that is, there is nothing logically different about asking a tree walker to start at the root directory (/) or /usr/home/partheban.

I also fear, however, that Richard is right, and that you may be running into simple file/directory access issues. And if that's the case, I would definitely check with your sysadmin before you start running anything that does indeed traverse every directory in your system. /proc, for starters (don't know if Ubuntu uses it), is one that I'm not at all sure I would want to be ploughing through - or, as a sysadmin, letting anyone traverse - /dev would be another.

Winston
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Partheban Udayakumar wrote:I am working in Ubuntu environment,so I have only one root.

I fear you and Richard are using the word "root" differently.

I use 'root' meaning both the 'root' user and the 'root' of the file system relying on the context to make it clear which is meant. I should be more explicit.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me, root means root folder.

Richard, I am sorry I tried as root in terminal but still there is problem.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:According to me, root means root folder.

Richard, I am sorry I tried as root in terminal but still there is problem.


Without us having a view of your code it would seem to be impossible to say what is wrong. Earlier I ran one of my utilities written in Java as the root user and it processed the whole file system without any problems. I still don't see what can be proprietary about your code that makes it impossible for you to post.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Richard, I am sorry I tried as root in terminal but still there is problem.

Question: have you tried
find / -type d -print
with the same sudo privileges?

If so, does it print out the same directories as you're getting with your Java program? If not, give us at least an example of a mismatch. That might help us; otherwise, like Richard says, we're kind of hamstrung, because we can't see your code.

Winston
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code:



But I am not able to access all the system files with this code. I tried all the suggested ways
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Here is my code:

You still haven't told us what happens. Do you get an Exception? Do you get any messages?

Like I say, I've never used it, but from the docs it looks to me as though you could possibly help yourself out by overriding the visitFileFailed() method and using it to print out relevant information before continuing.

Also: Please DontWriteLongLines. I makes your thread very hard to read. I've broken yours up this time, but for future reference:
80 characters max.

Thanks.

Winston
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Partheban Udayakumar wrote:Here is my code:

You still haven't told us what happens. Do you get an Exception? Do you get any messages?

Like I say, I've never used it, but from the docs it looks to me as though you could possibly help yourself out by overriding the visitFileFailed() method and using it to printing out relevant information before continuing.

Also: Please DontWriteLongLines. I makes your thread very hard to read. I've broken yours up this time, but for future reference:
80 characters max.

Thanks.

Winston



Exactly ! As expected the code as it stands throws

and terminates. As you say, this is simply solved by implementing the visitFileFailed() method, doing whatever the OP needs to do and returning FileVisitResult.CONTINUE .

I don't understand why the test class extends SimpleFileVisitor<Object> . I don't understand what in this code can be considered 'proprietary'.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!