import java.io.*;
import java.util.*;
public class Fichier {
static private Random rand = new Random();
private File nomFich;
public Fichier(String nom) {
nomFich = new File(nom);
}
/**
* Rôle : initialise le fichier courant avec n entiers tirés au hasard
*/
public void aléatoire(int n) {
try (DataOutputStream dos = new DataOutputStream
(new FileOutputStream(nomFich)))
{
for (int i=0; i<n; i++)
dos.writeInt(rand.nextInt(200));
}
catch(FileNotFoundException e) { System.err.println(e);}
catch(IOException e) { System.err.println(e); }
}
/**
* Rôle : renvoie le minimum du Fichier courant
*/
public int minimum() {
int min = Integer.MAX_VALUE;
try (DataInputStream di = new DataInputStream
(new FileInputStream(nomFich)))
{
while (true) {
int x = di.readInt();
if (x<min) min = x;
}
}
catch(EOFException e) {}
catch(FileNotFoundException e) { System.err.println(e);}
catch(IOException e) { System.err.println(e); }
finally {
return min;
}
}
/**
* Rôle : retourne la représentation de this sous forme d’une
* chaîne de caractères (String)
*/
public String toString() {
String s = "";
try (DataInputStream di = new DataInputStream
(new FileInputStream(nomFich)))
{
while (true)
// on concatène le prochain élément du fichier à s
s += di.readInt() + " ";
}
catch(EOFException e) {}
catch(FileNotFoundException e) { System.err.println(e);}
catch(IOException e) { System.err.println(e); }
finally {
return s;
}
déc. 06, 16 9:57 Page 1/2Fichier.java
Printed by Vincent Granet
mardi décembre 06, 2016 1/2
}
//
// Simple test de la classe Fichier
//
public static void main (String args[]) {
Fichier f = new Fichier("f.dat");
f.aléatoire(20);
System.out.println(f);
System.out.println(f.minimum());
}
}
déc. 06, 16 9:57 Page 2/2Fichier.java
Printed by Vincent Granet
2/2 mardi décembre 06, 2016
1 / 1 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !