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 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.
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 !
Joined: Oct 20, 2006
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.
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:
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