Récupérer des données formattées à partir d'un fichier txt
je veux recuperer le type de regle apartir d un fichier txt avec java. voila le fichier txt:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
donc je veu recuperer INPUT apres FORWARD et enfin OUTPUT.
voila mon code qui recupere les autre champs et je veu ajouter le type de chaque regle (INPUT ou FORWARD ou OUTPUT)
voila mon code:
Code:
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 68 69 70 71 72 73 74 75 76
| public class Remplire {
public static void affiche ( String ligne){
String []T= ligne.split(" ") ;
System.out.println("target ="+T[0]);
System.out.println(" prot: " +T[1]);
System.out.println(" opt :" +T[2]);
System.out.println(" source: " +T[3]);
System.out.println("destination : " +T[4]);
// System.out.println("state : " +T[5]);
// Session session = HibernateUtil.currentSession();
// Transaction tr=session.beginTransaction();
// Regles r = new Regles();
// r.setTarget(T[0]);
// r.setProt(T[1]);
// r.setOpt(T[2]);
// r.setDestination(T[4]);
// r.setSource(T[3]);
// session.save(r);
// tr.commit();
}
public static void main(String[] args){
String fichier ="c://f.txt";
boolean x ;
//lecture du fichier texte
try{
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
int i= 0,j = 0 ;
while ((ligne=br.readLine())!=null){
System.out.println("-->>"+ligne);
if (ligne.length() == 0 ){
j ++ ;
if (j > 1)
i = 0 ;
}
else if (ligne.substring(0,6).equals("target")) {
i = 1 ;
j = 0 ;}
else
j = 0 ;
if (i> 1 && j == 0)
affiche (ligne) ;
i++ ;
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
}
} |