A voir également:
- Fichier en java
- Jeux java itel - Télécharger - Jeux vidéo
- Fichier bin - Guide
- Waptrick java football - Télécharger - Jeux vidéo
- Fichier epub - Guide
- Fichier rar - Guide
1 réponse
On peut par exemple faire comme ceci :
Pour plus d'informations, regarde la documentation pour Scanner et FileOutputStream
La confiance n'exclut pas le contrôle
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
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
{
FileOutputStream f = new FileOutputStream(new File(fichier3));
f.write(ligne1.getBytes());
f.close();
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
return true;
}
Pour plus d'informations, regarde la documentation pour Scanner et FileOutputStream
La confiance n'exclut pas le contrôle
/*
* 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);
File R3 = new File("C:\\R2.txt") ;
BufferedWriter out = new BufferedWriter(new FileWriter(R3, true));
a1.insert1(a2);
a1.insert2(a2);
boolean b= copierPremiereLigneCommune();
}
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
{
FileOutputStream f = new FileOutputStream(new File(fichier3));
f.write(ligne1.getBytes());
f.close();
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
return true;
}
}
/* * 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); }} }/* * 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; }}