This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Couldn't check status of file/directory with casting error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Couldn Watch "Couldn New topic
Author

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

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Murly Kant
Greenhorn

Joined: Oct 18, 2010
Posts: 5
Hi Jack,
Christophe Verré is correct. Try this one

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 20, 2006
Posts: 235
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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Oct 20, 2006
Posts: 235
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

Joined: Jul 26, 2007
Posts: 10172
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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

Joined: Jul 26, 2007
Posts: 10172
    
    8

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

Joined: Aug 05, 2005
Posts: 3432
    
  12
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.

Joanne
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

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

Joined: Oct 20, 2006
Posts: 235
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

Joined: Aug 05, 2005
Posts: 3432
    
  12
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.
 
 
subject: Couldn't check status of file/directory with casting error
 
Similar Threads
Show dir and file structure exactly as it is
FileName to write to a .txt File
Java file I/O
delete files in a driectory
Unable to resolve Null Pointer Exception