Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couldn't check status of file/directory with casting error

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to search for all the files under certain directory with the following Java code with casting compilation error:

The compilation error on line 9 is as follows:

Exception in thread "main" java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at FileOperation.TraverseFilesDirectories.visitAllFiles(TraverseFilesDirectories.java:9)
at FileOperation.TraverseFilesDirectories.main(TraverseFilesDirectories.java:104)
Java Result: 1

I am having difficulty checking the status of each file/directory (children[i]) on line 9 using casting.

Any assistance would be much appreciated.

Thanks,

Jack>
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Casting a String into a File won't work.
2. I've never seen this type of casting. We usually write "((File)children[i]).

So, use listFiles() instead of list(). This will return you an array of Files, so you won't need to cast anything.
 
Murly Kant
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,
Christophe Verré is correct. Try this one

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Murly, and welcome to the ranch

We usually try to avoid giving full source answers to help ranchers. We think it's better to make people think a bit
And next time you post some code, please UseCodeTags. Have fun !
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe & Murly,

Thank you to both for offering your advices. I have got it working with the following code snippets:

Btw, my intention is to copy every file found to an archived folder including their directory structures. E.g. C:\Temp\Vehicle\Toyota\Lexus.xml to E:\Archived\Vehicle\Toyota\Lexus.xml. Is there a way to create the directory structure if it does not exist, similar to "mkdir –p" on Unix? Below is the code snippet I would start off with copying files but not sure whether this is the best approach:

Hope you do not mind answering this question which is not directly related to the subject any longer. I am more than happy to post another threat otherwise.

Thanks again,

Jack
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Bush wrote:Is there a way to create the directory structure if it does not exist, similar to "mkdir –p" on Unix?

You want to look at the File.mkdirs() method.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew,

Yes, File.mkdirs() is exactly what I was looking for. Thank you very much.

Promise to be the last question for this threat. I am also trying to list all the files with suffix of “doc” (*.doc) with the following code snippets without much luck so far:


Also tried “^*.doc”, “^*.*$doc”, “^*\\.doc”.

Have I missed something in regular expression? I am looking for files that ends with “doc” suffix only.

Many thanks again,

Jack
>
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why regex? I think that's an overkill.
You already got the file name right? Just check if the name ends with (hint) .doc and return the boolean accordingly.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh,

Thank for your suggestion.

Let's take a step back and try your approach, one that I started off earlier on without success by not using regular expression such as the following example:


Line 15 (instead of 16) would picked up all the files with different suffices. On the other hand, nothing was picked up when using line 16 (instead of 15) even though the pattern passed was “xml”, which suppose to find all files with suffix of “xml” only. E.g. C:\\Temp\\Vehicle\\Toyota\\Lexus.xml. I think the problem is that line 16 cannot be used as a pattern search (filter > “xml”) to locate both subdirectories (Vehicle, Toyota) when Lexus.xml lives 2 levels beneath these folders.

Line 16 (instead of 15) would work if the full path of where Lexus.xml is located, where there are no subdirectories to traverse further. i.e. line 30 would be as follows:

Please advice on what I should do.

Thanks,
Jack


 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You initialized the filter and did ....what? It just exists. Is any code calling the accept() ? Is the file filter really required?

In pseudo code
1) Check if given file is a directory
2) If file is a directory, get all its children and repeat for each child.
3) If file is not a directory, check if the file name satisfies the filter criteria
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:You initialized the filter and did ....what? It just exists. Is any code calling the accept() ? Is the file filter really required?


The filter is used on line 16. By passing it to the listFiles method, the listFiles method will use it to decide what files to include in the returned list.

Jack - you need to change your accept method so that it also returns true if the File is a directory. You will probably find that is easier if you use a FileFilter instead of a FilenameFilter.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
The filter is used on line 16. By passing it to the listFiles method, the listFiles method will use it to decide what files to include in the returned list.


Duh! My bad.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Maneesh,

Below is the simple solution (yet limits to one file search pattern but can be expanded) you have suggested earlier:

On the other hand, the solution suggested by Joanne Neal is slightly more complex but offers the flexibility of adding more file search patterns without changing underlying codes:


Both solutions work just as well.

Thank you very much for both of your guidances,

Jack
>
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
file.isDirectory() will always be true or false so there's no need to have it in the loop. I would do that test first before the loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic