• Post Reply Bookmark Topic Watch Topic
  • New Topic

simpleFileVisitor cant write to text file?  RSS feed

 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extended the SimpleFileVisitor class. And I am able to traverse the file directories. I give a file path then have it display the files full path and out put that to a text file. Here is what i have so far.









I am able to printout of the arraylist to the console. I cant get it to write to a text file whats in the arraylist to a text file. Any ideas on what im doing wrong?

Thank you for your time.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dade,
It works on my machine. What error message do you get when you run?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could it be a permissions issue? i know that sounds obvious, but you'd be amazed at the number of times that pops up...
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Dade,
It works on my machine. What error message do you get when you run?


When i look in the txt file the dir list from the arraylist isnt there. I am not getting any errors.

 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:could it be a permissions issue? i know that sounds obvious, but you'd be amazed at the number of times that pops up...


What do you mean by permissions?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
most operating systems allow for some kind of permissions on the files. every file in Unix, for example, has three - read permission, write permission, and execute permission. Each of these are defined for three classes - the file owner, the group, and the world. For example:

-rwxr-x--x 1 fred hci 832 Feb 24 10:47 ice_cream

means that "fred" has read, write and execute permission (the rwx) on the file "ice_cream"

The "hci" group has read an execute permission (the "r-x")

and everyone has execute permission (the "--x");

If you (or the user your program runs under) don't have permission to write to the file, you can't write to the file.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welp i figured it out after looking at it more i noticed it was being over written so i moved ArrayList outside of the method. and it works now. Anyone have any ideas how i could make this ALOT better?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. I tested with only one file so that didn't come up.

Given the idea is to only have one file, I think it is best not to do the writing in the visitor. Let the visitor just be responsible for adding values to the ArrayList. Then have the main method get the instance variable ArrayList from the visitor and print out the result once at the end.
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:I see. I tested with only one file so that didn't come up.

Given the idea is to only have one file, I think it is best not to do the writing in the visitor. Let the visitor just be responsible for adding values to the ArrayList. Then have the main method get the instance variable ArrayList from the visitor and print out the result once at the end.


I am unclear on how to implement that, can you please give me a code example?

Thank you for your help
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach is to have a print method that main calls. I actually like this better than what I originally suggested.



What I originally suggested was:
 
Dade Murphy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Very nice implementation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!