1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import java.io.File;
To change this template, choose Tools | Templates
and open the template in the editor.
class Parcours_fichiers {
private String chemin = "";
private int niveau = 0;
public int filecount = 0;
public int dircount = 0;
**Constructeur
public Parcours_fichiers(String path)
{
super();
this.chemin = path;
}
public void liste()
{
this.liste_Dossier(this.chemin, this.niveau);
}
public String repeat(final int itr,final String str){
if(itr>0){
return str + " " + repeat(itr-1,str);
} else{
return str ;
}
}
public void liste_Dossier(String dossier, int niveau) {
String chaine = repeat(niveau, "--");
String[] ext = new String[2];
ext[0] = ".txt" ;
ext[1] = ".jpg";
File file = new File(dossier);
File[] files = file.listFiles();
if (files != null)
{
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory() == true)
{
System.out.println(chaine+ files[i].getName());
this.dircount++;
}
else
{
for (int e = 0; e < ext.length; e++)
{
if (files[i].getName().endsWith(ext[e]))
System.out.println(chaine+files[i].getName());
this.filecount++;
}
}
if (files[i].isDirectory() == true )
{
this.liste_Dossier(files[i].getAbsolutePath(), niveau+1);
}
}
}
} |
Partager