Les fichiers en java

maha -  
 maha -
Bonjour,
ce code je le fait en java il à le meme principe que l 'operateur algebrique intersection entre 2 relation dans le base de donné en utilisant le fichier comme des relation et les fichier contient les attributs
svp je veux quelqu'un m 'aider à corriger ce code et l 'organiser
le classe relation est :

/*
 * 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 classe 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);

}}
}

et le main et le suivant
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package basedonné;



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);

a1.insert1(a2);
a1.insert2(a2);
   File fichier = new File("C:\\R3.txt") ;
BufferedWriter out = new BufferedWriter(new FileWriter(fichier, true));
out.close() ;
 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;
}








}
A voir également:

1 réponse

maha
 
svp aide moi à organiser se code et corriger l 'erreurs car il ne me permet pas de écrire dans le fichier 3
0