Вы находитесь на странице: 1из 7

Correction - TP 6 : Gestion des Fichiers

Créez le fichier "in.txt" dans le répertoire "D:\fichiers", rempli avec le contenu suivant

User1 passUser1
User2 passUser2
User3 passUser3

Exercice 1
Exécutez le programme suivant :

import java.io.*;
public class TestCopy {
public static void main(String[] args) throws IOException {
File inputFile = new File("c:\\fichiers\\in.txt");
File outputFile = new File("c:\\fichiers\\out.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
}
}

Exercice 2
Ecrivez un programme qui permet de :

 Lire le fichier « in.txt ».


 Saisir deux chaines de caractère (Login et Mot de passe).
 Vérifier l‘existence de login et mot de passe saisis dans le fichier « in.txt » e
affichant le message « Authentification réussi » si les login et mot de passe
trouvés, ou « PB Authentification » dans le cas contraire.

Indices :

 Ouvrir le fichier : BufferedReader(new FileReader("d:\\fichiers\\in.txt"))


 Découper une chaine à base d’un séparateur, exemple pour ‘/’ : chaine.split("/");

Mr. Mohamed Salah MEDDEB RSI21 1


Correction
import java.io.*;
public class TestPassword {
public static void main(String[] args) {
String l;
BufferedReader f;
boolean test = false;
try {
f = new BufferedReader(new FileReader("d:\\fichiers\\in.txt"));
String login = lireMot("Entrez le login : ");
String password = lireMot("Entrez le mot de passe : ");
l = f.readLine();
while (l != null) {
String[] words = l.split(" ");
if (words[0].equals(login) && words[1].equals(password)) { test = true; }
l = f.readLine();
}
f.close();
if (test == true) {
System.out.println("Authentification réussi");
} else {
System.out.println("PB Authentification");
}
} catch (IOException e) {
System.err.println("Erreur: " + e);
}
}
public static String lireMot(String msg) throws IOException {
BufferedReader keyboard;
keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.print(msg);
return keyboard.readLine();
}
}

Mr. Mohamed Salah MEDDEB RSI21 2


Exercice 3
Ecrivez un programme qui permet de :

 Lire le fichier « in.txt ».


 Compter le nombre des lignes, des chaines et de caractères du fichier lu.

Indice :
 Utilizer StringTokenizer pour lire une ligne mot par mot.
 La méthode countTokens permet de compter le nombre des mots d’une ligne.

Correction
import java.io.*;
import java.util.*;
public class TestFileStat {
public static void main(String[] args) {
String filename = "d:\\fichiers\\in.txt";
try {
countLines(filename);
}
catch (IOException e) {
System.out.println("Impossible d'ouvrir le fichier " + filename);
System.out.println(e);
}
}
//----------------------------------------------------------------------------
public static void countLines(String filename) throws IOException {
String line = "";
int nbLines = 0;
int nbWords = 0;
int nbChars = 0;
StringTokenizer st = null;
BufferedReader file;

Mr. Mohamed Salah MEDDEB RSI21 3


file = new BufferedReader(new FileReader(filename));
line = file.readLine();
while (line != null) {
st = new StringTokenizer(line);
nbLines++;
nbWords += st.countTokens();
nbChars += line.length() + 1; // +1 for the end-of-line char !
line = file.readLine();
}
file.close();
System.out.println("Number of lines : " + nbLines);
System.out.println("Number of words : " + nbWords);
System.out.println("Number of chars : " + nbChars);
}
}

Exercice 4
Reprenez l’exercice 2 et ajoutez une interface graphique pour la saisie de login et mot
de passe.

Mr. Mohamed Salah MEDDEB RSI21 4


Correction
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class TestPasswordGraphique extends Frame implements WindowListener,
ActionListener {
Label lbl1, lbl2, reultat, entete;
Button b1;
TextField txt1, txt2;
TestPasswordGraphique() {
super("Authentification");
setLayout(new BorderLayout());
Panel pnorth = new Panel();
pnorth.setLayout(new FlowLayout());
entete = new Label("-------- Authentification ---------");
pnorth.add(entete);
this.add(pnorth, BorderLayout.NORTH);

Panel pcenter = new Panel();


pcenter.setLayout(new GridLayout(2, 2));
lbl1 = new Label("Login :"); pcenter.add(lbl1);
txt1 = new TextField(); pcenter.add(txt1);
lbl2 = new Label("Mot de passe :"); pcenter.add(lbl2);
txt2 = new TextField(); pcenter.add(txt2);
//txt2.setEchoChar('*');
this.add(pcenter, BorderLayout.CENTER);

Panel psouth = new Panel();


psouth.setLayout(new GridLayout(2, 1));
b1 = new Button("Authentification");
b1.addActionListener(this);

psouth.add(b1);

Mr. Mohamed Salah MEDDEB RSI21 5


reultat = new Label("--------"); psouth.add(reultat);
this.add(psouth, BorderLayout.SOUTH);
addWindowListener(this);
this.pack();
this.show();
}
public static void main(String[] args) { new TestPasswordGraphique(); }
public void windowOpened(WindowEvent e) { }
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void actionPerformed(ActionEvent e) {
String l;
BufferedReader f;
boolean test = false;
try {
f = new BufferedReader(new FileReader("d:\\fichiers\\in.txt"));
l = f.readLine();
String login = txt1.getText();
String password = txt2.getText();
while (l != null) {
String[] words = l.split(" ");
System.out.println("---" + words[0] + "----");
System.out.println("---" + words[1] + "----");
if (words[0].equals(login) && words[1].equals(password)) {
test = true;
System.out.println("---C bon----");
}
l = f.readLine();
}
f.close();

Mr. Mohamed Salah MEDDEB RSI21 6


if (test == true) {
reultat.setText("Authentification réussi");
} else {
reultat.setText("PB Authentification");
}
} catch (Exception e1) {
System.err.println("Erreur: " + e1);
}
}
}

Mr. Mohamed Salah MEDDEB RSI21 7

Вам также может понравиться