Re: Counting file extension
Hello,
I have a code with me, see if this helps you
Code:
File fl = new File("Path of directory file properties");
File[] prls = fl.listFiles(new FileFilter() {
Public boolean accept(File file) {
if (file.getName().endsWith(. "properties"))
return true;
return false;
}
});
Re: Counting file extension
Hello,
This is my program, just have a look at it.
Code:
Public class CompteurDeFichiersAvecExtension
{
Public static void hand(String[] args)
{
File fl = new File(. / datas ");
File[] prls = fl.listFiles(new FileFilter()
{
Public boolean accept(File file)
{
if ((file.isDirectory()==false) &&
(file.getName().endsWith(. "properties")))
{
return true;
}
return false;
}
});
for(int i =0; I <prls.length; I + +)
{
String = nomAvecExt prls[i].getName();
int lg = nomAvecExt.length();
String nm = nomAvecExt.substring(0, Long-11);
System.out.System.out.println(nm);
}
}
}
Re: Counting file extension
Hello,
Check out this code, I think it can help you.
Code:
Public class CompteurDeFichiersAvecExtensionG
{
Public static void hand( Final String[] args)
{
File fl = null;
File[] prls = null;
String fld = null;
String fil = null;
if ( args.length == 0 ) { fld = "."; Filter = ""; }
else if ( args.length == 1 ) { fld = "."; Filter = args[0]; }
else { fld = args[0]; Filter = args[1]; }
fl = new File(fld);
if (( ! fil.startsWith(".") )&&( args.length != 0 )) fil = "."+ fil;
Final String tmp = fil;
prls = fl.listFiles(new FileFilter()
{
Public boolean accept(File file)
{
if ((file.isDirectory()==false) &&
(file.getName().endsWith(tmp)))
{
/ / Only files are made, not
/ / The directory.
return true;
}
return false;
}
});
for(int i =0; I <prls.length; I + +)
{
String = nomAvecExt prls[i].getName();
int lg = nomAvecExt.length();
String nomSansExt = nomAvecExt.substring(0, Long-fil.length());
System.out.System.out.println(nomSansExt);
}
System.out.System.out.println("\ n > "+ prls.length+"File *"+ + fil"Found.");
}
}
Re: Counting file extension
Hello,
I have a small question, How to catch this error if the folder is not referenced.
Code:
fl = new File (folder);
NullPointerException in thrown? Yet folder String is never empty. If the program does not find the folder, it crashes. So, I think I should mention a try and catch block in my code.
Re: Counting file extension
Hi,
You can make your code more reliable, by just including the following code.
Code:
File fl = new File(. / data ");
if (fl.exists()) {
doSomething();
} else {
doNothing();
}
If you have any more problems in this then do post by with your query.