Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to only list sub directories in JList?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I want to list only the sub directories of a specific directory, not including files. I used the following code, but files in the "mall" directory also are included.
File mallDir = new File("mall");
if(!mallDir.exists()){System.out.println(mallDir.getName() + " not found."); return;}
if (mallDir.isDirectory()){String[] store = mallDir.list();}
JList list1 = new JList(store);
JScrollPane storeList = new JScrollPane(list1);
Any suggetion? Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm no expert on IO so there may be a better way than this:
If you look at the API for File.list() you will see that it is overloaded to take a FilenameFilter. So you implement a FilenameFilter and override boolean accept( java.io.File , String ) so that it returns false for non-directories.
Try it and let me know if it works.
 
Frank John
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I tried hard this weekend, but still can't get it. I am not sure how to use this FilenameFilter to get Directory. Only example I found about this interface is in Thinking in Java. However, this Filter seems just to filter some specific files with specified name.
I tried to use another string array and a for loop to decide if the element in the original listed String array are file or directory. But out of luck, I always get false. I don't know why. Also I got NullPointerException.
Help...
String[] preStore, store;
if (mallDir.isDirectory()){
preStore = mallDir.list();
for(int i= 0; i<preStore.length; i++){
if(new File(preStore[i]).isDirectory())
store[i] = preStore[i];
}
}
list1 = new JList(Store);
 
Frank John
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Finally, I am able to list only the sub-directories under the directory "mall", no files are listed. The way I did it is kind of awkward. Don't know if there is any better idea, such as using filter?
Here is my clumsy but working code.
/* preStore is a string array which list both
dir and file, while store is a string array on
list dir */
String[] preStore, store;
//just check directory mall exits
File mallDir = new File("mall");
if(!mallDir.exists()){System.out.println(mallDir.getName() + " not found."); return;}
//to get dir list String[] store
if (mallDir.isDirectory()){
preStore = mallDir.list();
/*get the number of subdir and
construct the store array*/
int storeSize=0;
for(int i= 0; i<preStore.length; i++)
if(new File("mall"+File.separator+preStore[i]).isDirectory()) storeSize++;
store = new String[storeSize];
/* initialize the store string array */
int j=0;
for(int i= 0; i<preStore.length; i++)
if(new File("mall"+File.separator+preStore[i]).isDirectory()) store[j++] = preStore[i];
}
list1 = new JList(store);
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Frank,
Please do not double post. I know it is sometimes difficult to decide which forum to post to, but from now on, pick just one.
Here is my solution to your problem in the IO forum:
List only subdirectories

Michael Morris
[ March 02, 2003: Message edited by: Michael Morris ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi,
we can also use FileFilter inteface as,
class MyFileFilter implements FileFileter {
public boolean accept(File f) {
return f.isDirectory();
}
}
class test {
public static void main(String[] s) {
File f = new File("mydir");
String[] list = f.list(new MyFileFilter());
}
}

regards
maulin.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
As Michael suggested, let's continue this conversation in The I/O and Streams Forum...
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic