Spliter un fichier.txt

inama1 Messages postés 10 Date d'inscription lundi 26 mars 2012 Statut Membre Dernière intervention 18 mai 2012 - 15 mai 2012 à 16:03
 Utilisateur anonyme - 15 mai 2012 à 21:23
Bonjours,
J'ai réussi à splitter un fichier text
Mon fichier :
ping 172.22.152.60

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.22.152.60, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
DCN_SW_TUN9000_SRV#

Mais je veux maintenant afficher le champ qui se trouve après le mot : "percent" (aprés tester l'existance du mot "percent")
Merci pour votre aide et voici mon code :

public class parcing_ping {


public static void main(String[] args) {
File file = new File("C:\\ping1.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

while (dis.available() != 0) {

String ch=dis.readLine();
String[] tokens = ch.split(" ");


for (String t :tokens)
{
if((t.compareTo("percent")==0) )
System.out.println(t);}

}


fis.close();
bis.close();
dis.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

1 réponse

Utilisateur anonyme
15 mai 2012 à 21:23
Salut,

D'après ton exemple de fichier,
Si afficher le champ qui se trouve après le mot : "percent" c'est afficher l'item suivant:
remplace le code
for (String t :tokens)
               {
                  if((t.compareTo("percent")==0) )
                     System.out.println(t);}
[/code
par
[code=java]
for(int i = 0; i <tokens.length; i++)
               {
                  if(tokens[i].equals("percent"))
                     System.out.println("L'item  suivant 'percent' est " + tokens[i+1]);
               }


Sinon précise ce qu'est le champ qui suit...

Cordialement,
...\ Dan /...
0
Rejoignez-nous