Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

File count doubt in Java

Hi Experts...
I have a requirement where i am supposed to count the number of files with a particular extension.
For example i have a fille format of the extension aq11.ext.det.
How do i count the number of files with this extension of this file format.
Btw i work in a unix file system.
Please help...
0
gaugeta
Asked:
gaugeta
  • 5
  • 2
1 Solution
 
for_yanCommented:

File dir = new File("directory");
String [] fileNames = dir.list();
int count = 0;
for(String s : fileNames){
if(s.endsWith("ext"))count++l
}

Open in new window

0
 
for_yanCommented:
small corrrection:

File dir = new File("directory");
String [] fileNames = dir.list();
int count = 0;
for(String s : fileNames){
if(s.endsWith("ext"))count++;
}

Open in new window

0
 
for_yanCommented:

to be more specific if you want aq11.ext.det
File dir = new File("directory");
String [] fileNames = dir.list();
int count = 0;
for(String s : fileNames){
if(s.endsWith("aq11.ext.det"))count++;
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:

This is static method which will count number of files with given extension
in the given directory:

public static countFiles(String dirName, String ext){
int count = 0;
File dir = new File(dirName);
String [] fileNames = dr.list();
for(String s : fileNames){
if(s.endsWith(ext))count++;

}
return count;

}

Open in new window

0
 
gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
To answer your comment "to be more specific if you want aq11.ext.det" here .ext.det is the common extension aq11 is unique to every file.

0
 
for_yanCommented:
then you just change to

if(s.endsWith(".ext.det"))count++;

or provide ".ext.det" as the second argument of the above  method
0
 
gaugetaAuthor Commented:
@for_yan:Thanks for the reply.
I try this and get back if i face any issues.
0
 
niketpipaliyaCommented:
public static countFiles(String dirName, String ext){
int count = 0;
File dir = new File(dirName);
String [] fileNames = dr.list();
for(String s : fileNames){
if(s.endsWith(ext))count++;

}
return count;

}
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now