Comparaison entre fichiers
zmandar
-
zmandar -
zmandar -
Bonjour,
svp je veux faire un code en java qui me permet de comparer 2 fichier.txt ligne par ligne si le premier ligne de fichier 1 égal au 1 ligne de fichier 2 en crée un fichier 3 et en récupérer ce ligne dans le fichier 3 svp aide moi à faire ce code
svp je veux faire un code en java qui me permet de comparer 2 fichier.txt ligne par ligne si le premier ligne de fichier 1 égal au 1 ligne de fichier 2 en crée un fichier 3 et en récupérer ce ligne dans le fichier 3 svp aide moi à faire ce code
A voir également:
- Comparaison entre fichiers
- Vérificateur des fichiers système - Guide
- Comparaison million milliard - Accueil - Technologies
- Renommer des fichiers en masse - Guide
- Fichiers epub - Guide
- Explorateur de fichiers - Accueil - Windows
3 réponses
Salut,
Une recherche google t'aurait déjà aiguillé sur la piste de la lecture de fichiers en Java : https://forum.hardware.fr/hfr/Programmation/Java/inputstreamreader-lecture-fichiers-sujet_81619_1.htm
Une recherche google t'aurait déjà aiguillé sur la piste de la lecture de fichiers en Java : https://forum.hardware.fr/hfr/Programmation/Java/inputstreamreader-lecture-fichiers-sujet_81619_1.htm
Pourquoi avoir modifié mon code, alors qu'il marchait très bien ?
Arrête de poser 15 fois la même question, on ne te donnera pas 15 réponses différentes !
Arrête de poser 15 fois la même question, on ne te donnera pas 15 réponses différentes !
mais ce code il compare seulement le premier ligne de chaque fichier moi je veux qu'il test si le premier ligne existe dans le deuxième fichier alors en l 'ajouter dans le troisième fichier et en teste si le 2 ligne de 1 fichier existe dans le 2 fichier je l 'ajoute dans le 3 fichier etc svp aide moi
moi je charge mon fichier dans un tableau mais je ne sais pas pourquoi T3 il est est vide lorsque le test est true svp aide moi à le corriger
static void intersection()throws IOException{
String chaine="";
int i=0;
int j=0;
String []T1 ;
String [] T2;
String [] T3;
T1=new String [44];
T2=new String [44];
T3=new String [44];
String fichier1 ="C:\\R1.txt";
//lecture du fichier texte
try{
InputStream ips=new FileInputStream(fichier1);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
System.out.println(ligne);
T1[i]=ligne;
i++;
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
String fichier2 ="C:\\R2.txt";
//lecture du fichier texte
try{
InputStream ips=new FileInputStream(fichier2);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
System.out.println(ligne);
T2[i]=ligne;
j++;
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
for (int k=1 ; k<=3;k++){
String s1= T1[k];
// System.out.println(s1);
String s2=T2[k];
// System.out.println(s2);
int f=1;
if(s1.equals(s2)==true ){
T3[f]=s1;
System.out.println(T3[f]);
f++;
}}
// for (int h=1 ; h<=T3.length;h++){System.out.println(T3[h]);}
for (int h=1 ; h<=T3.length;h++){
FileWriter writer = null;
String texte = T3[h];
try{
writer = new FileWriter("C:\\R3.txt", true);
writer.write(texte);
}catch(IOException ex){
ex.printStackTrace();
}finally{
if(writer != null){
writer.close();
}
}
}
Les String[] ne sont pas adaptés à ton problème, j'ai utilisé ArrayList<String> à la place. De plus il faut structurer ton code, et surtout le décomposer en méthodes simples et réutilisables :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public static ArrayList<String> lireFichier(String nomFichier) throws FileNotFoundException
{
ArrayList<String> liste=new ArrayList<String>();
Scanner sc = new Scanner(new File(nomFichier));
while (sc.hasNextLine())
{
liste.add(sc.nextLine());
}
sc.close();
return liste;
}
public static void ecrireFichier(String nomFichier, ArrayList<String> t) throws IOException
{
FileOutputStream out = new FileOutputStream(new File(nomFichier));
for (String s : t)
{
out.write(s.getBytes());
}
out.close();
}
static void intersection(String nomFichier1, String nomFichier2, String nomFichier3) throws IOException
{
ArrayList<String> t1=lireFichier(nomFichier1);
ArrayList<String> t2=lireFichier(nomFichier2);
ArrayList<String> t3=new ArrayList<String>();
for (String s1:t1)
{
for (String s2:t2)
{
if (s1.equals(s2))
{
//t1.remove(s1);
//t2.remove(s2);
t3.add(s1);
}
}
}
ecrireFichier(nomFichier3,t3);
}
public static void main(String args[])
{
try
{
intersection("D:\\test1.txt","D:\\test2.txt","D:\\test3.txt");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
je ne sais pas pourquoi toujours le fichier 3 est vide il ne écrire rien dans ce fichier même si il y 'a des ligne communs entre le 2 fichier l'objectif de mon code ce qu'il doit faire le même fonction que l 'opérateur algébrique intersection entre 2 relation comme en sql svp aide moi car personne ne veux m 'aider et je doit le terminer svp
voila tout le code
la classe relation
la clase attribut
le main
voila tout le code
la classe relation
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basedonné;
/**
*
* @author Administrateur
*/
public class relation {
private String nomRelation ;
private attribut Att1;//référence vers une instance de la class attribut
private attribut Att2;
public relation(String nomRelation , attribut Att1,attribut Att2 ){
this.nomRelation=nomRelation;
this.Att1=Att1;
this.Att2=Att2;
}}
la clase attribut
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basedonné;
import java.io.*;
public class attribut {
private String nom ;
private String type;
private int longueur ;
public attribut(String nom,String type,int longueur){
this.nom=nom;
this.type=type;
this.longueur=longueur;
}
public static void fichier1( String chaine,int cin) throws IOException
{
File fichier = new File("C:\\R1.txt") ;
BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
String c = String.valueOf(chaine);
String x = String.valueOf(cin);
out.write(c+" "+x);
out.newLine();
out.close() ;
}
void insert1(attribut a2) throws IOException{
String s ;
int cin ;
for (int i=1;i<=3;i++){
do
{ System.out.println ("donner un nom pour le relation 1");
s = Clavier.lireString ();
}while( s.length()>15);
System.out.println("entre num de cin pour le relation 1 ") ;
cin=Clavier.lireInt ();
fichier1 (s,cin);
}}
public static void fichier2 ( String s,int k) throws IOException
{
File fichier = new File("C:\\R2.txt") ;
BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
String h= String.valueOf(s);
String m = String.valueOf(k);
out.write(h+" "+m);
out.newLine();
out.close() ;
}
void insert2(attribut a2) throws IOException{
String s ;
int cin ;
for (int i=1;i<=3;i++){
do
{ System.out.println ("donner un nom pour le relation 2");
s = Clavier.lireString ();
}while( s.length()>15);
System.out.println("entre num de cin pour le relation 2") ;
cin=Clavier.lireInt ();
fichier2 (s,cin);
}}
}
le main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basedonné;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String arg[]) throws IOException {
attribut a1 ;
attribut a2 ;
a1=new attribut ("nom","String",15);
a2=new attribut ("cin","int",12);
relation R1;
R1=new relation("E1",a1,a2) ;
relation R2;
R2=new relation("E2",a1,a2);
File fichier = new File("C:\\R3.txt") ;
BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
out.close() ;
a1.insert1(a2);
a1.insert2(a2);
intersection("C:\\ R1","C:\\ R2" , "C:\\ R3") ;}
public static ArrayList<String> lireFichier(String nomFichier) throws FileNotFoundException
{
ArrayList<String> liste=new ArrayList<String>();
Scanner sc = new Scanner(new File(nomFichier));
while (sc.hasNextLine())
{
liste.add(sc.nextLine());
}
sc.close();
return liste;
}
public static void ecrireFichier(String nomFichier, ArrayList<String> t) throws IOException
{
FileOutputStream out = new FileOutputStream(new File(nomFichier));
for (String s : t)
{
out.write(s.getBytes());
}
out.close();
}
static void intersection(String nomFichier1, String nomFichier2, String nomFichier3) throws IOException
{
ArrayList<String> t1=lireFichier(nomFichier1);
ArrayList<String> t2=lireFichier(nomFichier2);
ArrayList<String> t3=new ArrayList<String>();
for (String s1:t1)
{
for (String s2:t2)
{
if (s1.equals(s2))
{
//t1.remove(s1);
//t2.remove(s2);
t3.add(s1);
}
}
}
ecrireFichier(nomFichier3,t3);
}
}
String fichier1 ="R1.txt"; String fichier2 ="R2.txt"; String fichier3 ="R3.txt"; boolean b=copierPremiereLigneCommune(fichier1,fichier2,fichier3);} public static String premiereLigne(String fichier) throws FileNotFoundException { Scanner sc = new Scanner(new File(fichier)); String s = sc.nextLine(); sc.close(); return s; } /** * @param fichier1 fichier dont on lit la première ligne * @param fichier2 fichier dont on lit la première ligne * @param fichier3 fichier où l'on écrit la première ligne * @return false si l'opération a générée une exception, true sinon */ public static boolean copierPremiereLigneCommune(String fichier1, String fichier2, String fichier3) { String ligne1,ligne2; try { ligne1 = premiereLigne(fichier1); ligne2 = premiereLigne(fichier2); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } if (ligne1.equals(ligne2)) { try { FileWriter writer = null; String texte = ligne1; try{ writer = new FileWriter("c:\\R3.txt", true); writer.write(texte,0,texte.length()); }catch(IOException ex){ ex.printStackTrace(); }finally{ if(writer != null){ writer.close(); } } } catch (IOException e) { e.printStackTrace(); return false; } } return true; }