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
|
import java.io.*;
import java.util.*;
public class Td1 {
public static int compter(File f) throws Exception{
j'associe mon fichier à un scanner
Scanner s= new Scanner(f) ;
int c=0;
//je boucle jusqu'a la fin du fichier
while (s.hasNext()) {
// je lit le mot
s.next();
c++;
}
return c;
}
public static String[] crée(File f) throws Exception {
//d'abord je crée un tableau dans le quel je vais stock mon fichier
String[] t = new String[compter(f)];
//ensuite j'associe mon fichier au scanner pour pouvoir le lire.
Scanner s=new Scanner(f);
//je boucle jusqu'a la fin du fichier.
for(int i=0;i<t.length;i++)
//je le lit à partir su fichier et je le stock dans une itération du tableau.
t[i]=s.next();
//je retourne le tableau .
return t;
}
public static void main (String args[]) throws Exception{
// ma méthode crée à besoin d'un fichier à fin que je puisse la teste
//je saisi un fichier à partir de l'entrée standard
Scanner in=new Scanner(System.in);
//je lit le chemin du fichier
String s=in.next();
// je le passe comme paramétre à la méthode compter et puis je l'affiche jusqu'ici c'est bien */
System.out.println(compter(new File(s)));
// je veux tester si le tableau à éte crée mais aucune idée???
System.out.println(crée(new File(s))+"\t");
}
} |
Partager