*
The moose likes I/O and Streams and the fly likes File filter without empty directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File filter without empty directory" Watch "File filter without empty directory" New topic
Author

File filter without empty directory

Donald Fung
Greenhorn

Joined: Oct 27, 2004
Posts: 18
Hi,

I would like to write a program that get the info of all html. The program will search the html under the specific directory and sub-directories and sort them in order. The final output will be used to constructs a tree to user.
I have problem that how can I eliminate the empty directory with no html file inside.

Hope somebody can help. Thanks.




The sample output:

banner::G:\template\banner
fbarA.htm::G:\template\banner\fbarA.htm
fbarB.htm::G:\template\banner\fbarB.htm
profilebar.htm::G:\template\profilebar.htm
user_images::G:\user_images // I would like to eliminstae these directory
user_images_b::G:\user_images\temp
src::G:\Project\ntms2\src
V0::G:\Project\ntms2\src\V0
V1::G:\Project\ntms2\src\V1
jsp::G:\Project\ntms2\src\V1\jsp
servlet::G:\Project\ntms2\src\V1\servlet
config::G:\Project\ntms2\src\V1\servlet\config
mypackage::G:\Project\ntms2\src\V1\servlet\mypackage
[ February 21, 2005: Message edited by: Donald Fung ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Donald Fung:
This is the entire loop over the list of files for a given directory. Since line 4 traverses down a directory, line 2 is being executed for directories as well as htm/html files.
Donald Fung
Greenhorn

Joined: Oct 27, 2004
Posts: 18
Originally posted by David Harkness:
<hr></blockquote>This is the entire loop over the list of files for a given directory. Since line 4 traverses down a directory, line 2 is being executed for directories as well as htm/html files.


Thanks for your reply.
If then, I think the following can solve the problem.

1. I need to check if there're files under the directory and return a boolean by getDirectoryList().
2. Add if statement after the line 4 to check if there's file under the directory. If true, do nothing; else, delete the object added.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You shouldn't need to change getDirectoryList's signature.

How about splitting apart your file testing so each method tests one aspect. Currently accept() returns true for directories or htm/html files. The File class has a method that performs the first test already, so simply remove it.

Did you just add the "isFolder = files[i].isDirectory();" line or did I accidentally delete it when I copying? Either way, simply use this boolean to decide whether or not to put "add the file info to an array of object." No sense adding it and then removing it later if it's a directory when you already know whether or not it's a directory.

You can simplify your structure a bit by rearranging the lines you have. If you change checkFile() as I suggested, you'll need to. Simplify the following logic.
Donald Fung
Greenhorn

Joined: Oct 27, 2004
Posts: 18
Originally posted by David Harkness:
You shouldn't need to change getDirectoryList's signature.

How about splitting apart your file testing so each method tests one aspect. Currently accept() returns true for directories or htm/html files. The File class has a method that performs the first test already, so simply remove it.



I'd already test it yet, but I observerd that if I do not return True for directory. The listFiles() will filter out all sub-directory. So, I need to add it there.


Did you just add the "isFolder = files[i].isDirectory();" line or did I accidentally delete it when I copying? Either way, simply use this boolean to decide whether or not to put "add the file info to an array of object." No sense adding it and then removing it later if it's a directory when you already know whether or not it's a directory.


As I need to construct the file tree, I need to put the folder as a file object before the child files.

banner::G:\template\banner
fbarA.htm::G:\template\banner\fbarA.htm
fbarB.htm::G:\template\banner\fbarB.htm


You can simplify your structure a bit by rearranging the lines you have. If you change checkFile() as I suggested, you'll need to. Simplify the following logic.


David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
[ Midway through posting this I realized i misunderstood your original issue. You can safely skip down to the "LOL, looks like..." paragraph, but I still think the rest is worthwhile as general advice. ]
Originally posted by Donald Fung:
That isn't the same as what I posted -- you've simply removed a test that makes a difference. What I was suggesting was to write something that is symantecally the same but in a simpler form.There are a few simplifications you can make. First, line 2 cannot be true because line 1 found that A is true, thus !A is false, and line 3 will not execute; move lines 2 and 3 outside if block 1.With this move, at line 3 we know that line 1 failed, thus A is true. This makes testing A redundant; remove the "A &&".The same can be applied to line 4 since it's inside the same if block. A must be true; remove "A &&".Okay that's nice and all, but how does this apply to your code? It should simplify the logic of getDirectoryList().
As I need to construct the file tree, I need to put the folder as a file object before the child files.

banner::G:\template\banner
fbarA.htm::G:\template\banner\fbarA.htm
fbarB.htm::G:\template\banner\fbarB.htm
I cannot tell what exactly you mean here. Are you saying that you want the directory entry in the resulting list of files? Then I totally misunderstood your original problem.
I have problem that how can I eliminate the empty directory with no html file inside.
LOL, looks like that's the case. I thought you wanted all directories removed! Okay, easy fix to your original code.Adding the same thing to the simplified code results in this.Remember that if you use my modified code, you need to change checkFile()/acceptFile() so that it returns false for directories instead of true. I apologize for steering you in the wrong direction there, I must have skipped over the part where you said you wanted to create a directory tree and thought you just wanted a list of htm/html files.
[ February 21, 2005: Message edited by: David Harkness ]
Donald Fung
Greenhorn

Joined: Oct 27, 2004
Posts: 18
That's what I want.I'll try the code.

One more question to ask, seems that filenameFilter will filter out file which does not fulfill the String and all directory. Isn't it?
[ February 21, 2005: Message edited by: Donald Fung ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Donald Fung:
One more question to ask, seems that filenameFilter will filter out file which does not fulfill the String and all directory. Isn't it?
I just noticed that this class implements FilenameFilter. Are you using this in a Swing File Open dialog? If so, I don't think it deals with a recursive directory tree, but then again I haven't used Swing in a long time.

Anyway, can you be more specific about your question? Whose code are you talking about? And how are you testing this; what output do you see and where? The last changes I suggested should add the directory as long as it has at least one htm/html file in it.

Ack, that won't work either! It will not show the root directory if it only has other directories with htm/html files in them.

I'm submitting this and I'll see if I can type of a brief description of the solution that seems easiest.

In the future, try not to post new questions by modifying a post since they don't show up the same way. It's usually best to leave editing for when you made a mistake in your post and need to correct it.
Donald Fung
Greenhorn

Joined: Oct 27, 2004
Posts: 18
Thanks for your suggestion.
I brief my question more clear.

I have to build a web that allow user to access some pages from the server. I had complete the basic function.

But I had some problem for my output.

Remark: A, B, D, F & G are directories

A
|
B ----- C.html
| |
| --- D ---- E.html
|------ F ---- G

My current output is:
A::A
B::A\B
C.html::A\B\C.html
D::A\B\D
E.html::A\B\D\E.html
F::A\B\F
G::A\B\F\G

My desired output:
A::A
B::A\B
C.html::A\B\C.html
D::A\B\D
E.html::A\B\D\E.html

The parent directory (contained desired file) should be included in the output.
I'm finding ways to remove the "F::A\B\F" & "G::A\B\F\G" from the output.
[ February 22, 2005: Message edited by: Donald Fung ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Thank you, that was very clear. I'm on the same page as you now.

What you need to add is the ability to hold off adding a string of parent directories to the tree until you've found the first matching file. At that point, you want to add any directories that you haven't added yet and then add the file.

A recursive algorithm has three pieces: the code to initialize it (sometimes unnecessary), the recursive step (the one that calls itself), and the terminal step. Let's apply this to your algorithm and see what modifications are necessary.

To start the algorithm, you specify a root directory by creating a File. Since there may be no matching files, you don't want to add this directory to the list yet. After marking it as not yet added (don't worry about how for now), you call getDirectoryList(0, root). I'm assuming layer (level) starts at 0 and am passing in the File instead of its path. Better to stick with Files the whole way through instead of converting back and forth to paths.

The method gets an array of Files and iterates over it. If f is a readable directory, it recurses with getDirectoryList(layer+1, f). If f is a non-matching file, it is ignored.

Otherwise it has a matching file. Before adding f to the list, you must add any parent directories that haven't been added yet. Since you always add all directories above the current that haven't been added yet, at all times the directories waiting to be added are a contiguous chain of zero or more parent directories.On are the directories that have been added to the list so far. Off are those in the branch that are waiting to be added. Added are the directories and matching files added to the result list at that step.

When a matching file is found, any directories in the Off List are added to the result list and moved to the On List before adding the file itself.

When you go down a directory layer, you have to add that directory to the Off List. When you come up a layer, you must remove it from the On or Off List, wherever it is. The two lists combined form a single stack with a pointer to the deepest directory that has been added.

There are many ways you can implement that, but I think Lists will give you what you need. While it operates like a stack, you need random access to the elements within without removing them.

[ Added notes to table and improved sample tree. ]
[ February 22, 2005: Message edited by: David Harkness ]
 
 
subject: File filter without empty directory
 
Similar Threads
Reading dir
Compare two directories
Problem in File Browser
recursive methods
Java File IO