Gestion des exceptions pour louverture de fichier

This commit is contained in:
2024-05-04 16:54:22 +02:00
parent f5d9276a33
commit 2ab5388dc1
11 changed files with 12 additions and 4 deletions
+12 -4
View File
@@ -1,6 +1,8 @@
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@@ -32,11 +34,15 @@ public class Grid {
} }
System.out.println("Success"); System.out.println("Success");
System.out.println(this); System.out.println(this);
} catch (FileNotFoundException e) {
System.err.println("Erreur : Fichier non trouvé: " + e.getMessage());
} catch (EOFException e) {
System.err.println("Erreur : Fin de fichier atteinte prématurément: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error: " + e.getMessage()); System.err.println("Erreur d'entrée/sortie: " + e.getMessage());
} }
} }
public void saveGridToFile(String fileName) { public void saveGridToFile(String fileName) {
try (DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName))) { try (DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName))) {
for (int ligne = 0; ligne < 9; ligne++) { for (int ligne = 0; ligne < 9; ligne++) {
@@ -48,8 +54,10 @@ public class Grid {
output.writeInt(Integer.parseInt(line.toString())); output.writeInt(Integer.parseInt(line.toString()));
} }
System.out.println("Grille sauvegardée avec succès dans " + fileName); System.out.println("Grille sauvegardée avec succès dans " + fileName);
} catch (FileNotFoundException e) {
System.err.println("Erreur : Fichier non trouvé: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
System.err.println("Erreur lors de la sauvegarde de la grille: " + e.getMessage()); System.err.println("Erreur d'entrée/sortie: " + e.getMessage());
} }
} }
@@ -73,4 +81,4 @@ public class Grid {
System.out.println(); System.out.println();
} }
} }
} }
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.