Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 12th, 2008, 11:14 AM
bwfc01 bwfc01 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 7 bwfc01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 sec
Reputation Power: 0
How To Return Only Files NOT folders????

Hi,

The below method is used to return the contents of a directory.

The problem I am having is my program is only interested in the actual files, he folders are irrelevant.

Currently my program is reading-in a hardcoded file name and I am trying to make it read in any file.

Can somebody be kind enough to suggest a way on how I can achieve this.

Many Thanks

Code:
public String[] filesList()
	{
		File dir = new File(System.getProperty("filelocation"));
		String[] filesList = dir.list();
		
		if(dir.exists())
		{
			System.out.println(filesList.length + " ..Files Found");
	
			//for(int t = 0; t<filesList.length; t++)
			//{
			//	System.out.println(filesList[t]);
			//}
			
		}
		else
		{
			System.out.println("Directory not found");
			mLogger.info("Directory not found");
		}

		return filesList;
	}
}

Reply With Quote
  #2  
Old January 23rd, 2008, 05:31 PM
paulscode paulscode is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Fort Meade, MD
Posts: 36 paulscode User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 33 m 30 sec
Reputation Power: 1
Filtering out the Directories

One way would be to create a class like:
Code:
import java.io.FilenameFilter;
import java.lang.String;  
public class JustFiles implements FilenameFilter
{
    public boolean accept( java.io.File file, java.lang.String g )
    {
        return !file.isDirectory();
    }
};

Then in your code, you would have
Code:
File dir = new File(System.getProperty("filelocation"));
JustFiles theFiles = new JustFiles();
String[] filesList = dir.listFiles( theFiles );

I hope this helps.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > How To Return Only Files NOT folders????


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway