|
4 | 4 | import java.io.*; |
5 | 5 |
|
6 | 6 | public class ExceptionCustomizada_1 { |
7 | | - /*public static void main(String[] args) { |
8 | | - String nomeDoArquivo = "romances-blake-crouch.txt"; |
9 | | - imprimeArquivoNoConsole(nomeDoArquivo); |
10 | | - System.out.println("Vai chegar nessa linha independente de qualquer coisa!"); |
| 7 | + public static void main(String[] args) { |
| 8 | + String nomeDoArquivo = JOptionPane.showInputDialog("Nome do arquivo a ser exibido: "); |
| 9 | + |
| 10 | + imprimirArquivoNoConsole(nomeDoArquivo); |
| 11 | + System.out.println("\nCom exception ou não, o programa continua..."); |
11 | 12 | } |
12 | 13 |
|
13 | | - private static void imprimeArquivoNoConsole(String nomeDoArquivo) { |
14 | | - File file = new File(nomeDoArquivo); |
| 14 | + public static void imprimirArquivoNoConsole(String nomeDoArquivo) { |
15 | 15 |
|
16 | | - try{ |
17 | | - BufferedReader br = lerArquivo(file.getName()); |
| 16 | + try { |
| 17 | + BufferedReader br = lerArquivo(nomeDoArquivo); |
18 | 18 | String line = br.readLine(); |
19 | | -
|
20 | 19 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
21 | | -
|
22 | | - do{ |
| 20 | + do { |
23 | 21 | bw.write(line); |
24 | 22 | bw.newLine(); |
25 | | - line=br.readLine(); |
26 | | - } while(line != null); |
| 23 | + line = br.readLine(); |
| 24 | + } while (line != null); |
27 | 25 | bw.flush(); |
28 | | - } catch (FileNotFoundException e) { |
29 | | - JOptionPane.showMessageDialog(null, "Arquivo não existe: " + e.getMessage()); |
30 | | - e.printStackTrace(); |
31 | | - } catch (IOException e) { |
| 26 | + br.close(); |
| 27 | + } catch (ImpossivelAberturaDeArquivoException e) { |
| 28 | + JOptionPane.showMessageDialog(null, |
| 29 | + e.getMessage()); |
32 | 30 | e.printStackTrace(); |
| 31 | + } catch (IOException ex) { |
| 32 | + JOptionPane.showMessageDialog(null, |
| 33 | + "Ocorreu um erro não esperado, por favor, fale com o suporte." + ex.getMessage()); |
| 34 | + ex.printStackTrace(); |
33 | 35 | } |
34 | 36 | } |
35 | 37 |
|
36 | | - private static BufferedReader lerArquivo(String nomeDoArquivo) { |
37 | | - return new BufferedReader(new FileReader(nomeDoArquivo)); |
| 38 | + public static BufferedReader lerArquivo(String nomeDoArquivo) throws ImpossivelAberturaDeArquivoException { |
| 39 | + File file = new File(nomeDoArquivo); |
| 40 | + try { |
| 41 | + return new BufferedReader(new FileReader(nomeDoArquivo)); |
| 42 | + } catch (FileNotFoundException e) { |
| 43 | + throw new ImpossivelAberturaDeArquivoException(file.getName(), file.getPath()); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class ImpossivelAberturaDeArquivoException extends Exception { |
| 49 | + |
| 50 | + private String nomeDoArquivo; |
| 51 | + private String diretorio; |
| 52 | + |
| 53 | + public ImpossivelAberturaDeArquivoException(String nomeDoArquivo, String diretorio) { |
| 54 | + super("O arquivo " + nomeDoArquivo + " não foi encontrado no diretório " + diretorio); |
| 55 | + this.nomeDoArquivo = nomeDoArquivo; |
| 56 | + this.diretorio = diretorio; |
| 57 | + } |
| 58 | + |
| 59 | + /*@Override |
| 60 | + public String toString() { |
| 61 | + return "ImpossivelAberturaDeArquivoException{" + |
| 62 | + "nomeDoArquivo='" + nomeDoArquivo + '\'' + |
| 63 | + ", diretorio='" + diretorio + '\'' + |
| 64 | + '}'; |
38 | 65 | }*/ |
39 | 66 |
|
40 | 67 | } |
| 68 | + |
| 69 | + |
| 70 | + |
0 commit comments