Concaténer des fichiers

Résolu/Fermé
Guy - 29 févr. 2012 à 20:18
 Guy - 29 févr. 2012 à 22:59
Bonjour,

Voila mon problème j'ai un fichier A :

A 1 16258135 16258139
A 2 16258140 16258145
A 3 16258146 16258151

et un fichier B :

22 16258135 13
22 16258136 13
22 16258137 13
22 16258138 13
22 16258139 13
22 16258140 12
22 16258141 12
22 16258142 12
22 16258143 12
22 16258144 13
22 16258145 16

Et j'aimerais avoir dans un fichier qui regroupe l'information de cette manière sachant que dans le 1er fichier 16258135 et 16258139 représente les bornes de l'intervalle dans lequel est situé le nombre situé dans la 2ème colonne du fichier B:

A 1 16258135 16258139 22 16258135 13
A 1 16258135 16258139 22 16258136 13
A 1 16258135 16258139 22 16258137 13
A 1 16258135 16258139 22 16258138 13
A 1 16258135 16258139 22 16258139 13
A 2 16258140 16258145 22 16258140 12
A 2 16258140 16258145 22 16258141 12
A 2 16258140 16258145 22 16258142 12
A 2 16258140 16258145 22 16258143 12
A 2 16258140 16258145 22 16258144 13
A 2 16258140 16258145 22 16258145 16
A 3 16258146 16258151 22 16258146 14
A 3 16258146 16258151 22 16258147 16
A 3 16258146 16258151 22 16258148 16
A 3 16258146 16258151 22 16258149 18

Je n'ai presque jamais touché à la programmation et j'ai énormément besoin de ce fichier sachant que vu la taille c'est impossible à la main. Pouvez-vous m'aider ?

Merci énormément par avance pour votre aide
A voir également:

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
Modifié par KX le 29/02/2012 à 21:44
Avec les deux A et B que tu as donné, on devrait plutôt obtenir ceci :

A 1 16258135 16258139 22 16258135 13 
A 1 16258135 16258139 22 16258136 13 
A 1 16258135 16258139 22 16258137 13 
A 1 16258135 16258139 22 16258138 13 
A 1 16258135 16258139 22 16258139 13 
A 2 16258140 16258145 22 16258140 12 
A 2 16258140 16258145 22 16258141 12 
A 2 16258140 16258145 22 16258142 12 
A 2 16258140 16258145 22 16258143 12 
A 2 16258140 16258145 22 16258144 13 
A 2 16258140 16258145 22 16258145 16 

Si tu sais compiler du code Java c'est cadeau (sinon, renseigne toi ;)

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.LinkedList; 
import java.util.Scanner; 

class A 
{ 
    final String lettre; 
    final int num; 
    final long debut, fin; 
     
    public A(String s) { 
        String[] tab = s.split("\\s+"); 
        lettre=tab[0]; 
        num = Integer.parseInt(tab[1]); 
        debut=Long.parseLong(tab[2]); 
        fin=Long.parseLong(tab[3]); 
    } 
     
    public static LinkedList<A> lire(String fileName) throws FileNotFoundException { 
        LinkedList<A> liste = new LinkedList<A>(); 
        Scanner sc = new Scanner(new File(fileName)); 
        while (sc.hasNextLine()) 
            liste.add(new A(sc.nextLine())); 
        sc.close(); 
        return liste; 
    } 
     
    public String toString() { 
        return lettre+" "+num+" "+debut+" "+fin; 
    } 
} 

class B 
{ 
    final int num1, num2; 
    final long val; 
     
    public B(String s) { 
        String[] tab = s.split("\\s+"); 
        num1 = Integer.parseInt(tab[0]); 
        val=Long.parseLong(tab[1]); 
        num2=Integer.parseInt(tab[2]); 
    } 
     
    public static LinkedList<B> lire(String fileName) throws FileNotFoundException { 
        LinkedList<B> liste = new LinkedList<B>(); 
        Scanner sc = new Scanner(new File(fileName)); 
        while (sc.hasNextLine()) 
            liste.add(new B(sc.nextLine())); 
        sc.close(); 
        return liste; 
    } 
     
    public String toString() { 
        return num1+" "+val+" "+num2; 
    } 
} 

class AB 
{ 
    private static final String endl = System.getProperty("line.separator"); 
     
    public static boolean accept(A a,B b) { 
        return b.val>=a.debut && b.val<=a.fin; 
    } 
     
    public static String toString(A a, B b) { 
        return a+" "+b+endl; 
    } 
     
    public static int fusionner(String fileNameA, String fileNameB, String fileNameAB) throws IOException { 
        LinkedList<A> listeA = A.lire(fileNameA); 
        LinkedList<B> listeB = B.lire(fileNameB);         
        FileOutputStream fic = new FileOutputStream(fileNameAB);         
        int n=0; 
        for (A a : listeA) 
        for (B b : listeB) 
            if (accept(a,b)) 
            { 
                fic.write(toString(a,b).getBytes()); 
                n++; 
            }         
        fic.close(); 
        return n; 
    } 
} 

public class Test 
{ 
    public static void main(String...args) throws IOException { 
        int n = AB.fusionner("D:\\A.txt","D:\\B.txt","D:\\C.txt"); 
        System.out.println(n+" correspondances."); 
    } 
}
La confiance n'exclut pas le contrôle
0
Merci infiniment !!!!
J'ai compilé et j'ai lu un peu le code afin de comprendre, c'est génial merci beaucoup !!!
0