Files
DEV/DEV3.2/TP06/02_Couleurs/LectureFichier.java

48 lines
1.2 KiB
Java
Raw Normal View History

2025-11-06 15:20:51 +01:00
import java.io.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
import java.awt.*;
public class LectureFichier {
private Map<String, Color> dictCouleurs;
public LectureFichier() {
this.dictCouleurs = new HashMap<>();
try {
BufferedReader lecture = new BufferedReader(new FileReader("rgb.txt"));
try {
String ligne = lecture.readLine();
while(ligne != null) {
String r = ligne.substring(0, 3).trim();
String g = ligne.substring(4, 7).trim();
String b = ligne.substring(8, 11).trim();
String nomCouleur = ligne.substring(11).strip();
this.dictCouleurs.put(nomCouleur, new Color(Integer.parseInt(r), Integer.parseInt(g), Integer.parseInt(b)));
ligne = lecture.readLine();
}
} catch(IOException e) {
System.err.println("Erreur de lecture dans rgb.txt !");
}
try {
lecture.close();
} catch(IOException e) {
System.err.println("Erreur de fermeture de rgb.txt !");
}
} catch(FileNotFoundException e) {
System.err.println("Erreur d'ouverture de rgb.txt !");
}
}
public Map<String, Color> getDictCouleurs() {
return this.dictCouleurs;
}
}