Advertisement
Victoralm

Exceções(Try/Catch/Finally)

Feb 20th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class t5 {
  4.  
  5.   public static void main(String[] args) {
  6.  
  7.     String os = System.getProperty("os.name");
  8.     Runtime run = Runtime.getRuntime();
  9.     String comando;
  10.  
  11.     if (os.matches("Linux")) {
  12.  
  13.       try {
  14.         comando = "touch " + System.getProperty("user.home") + "/Documentos/cadastrosAlms.txt";
  15.         run.exec(comando);
  16.         System.out.println("Arquivo \"cadastrosAlms.txt\" criado em " + System.getProperty("user.home") + "/Documentos");
  17.       } catch (IOException ex) {
  18.         System.out.println("Não foi possível criar o arquivo");
  19.         System.out.println("\terro: " + ex);
  20.       }
  21.       finally {
  22.         System.out.println("Encerrando a execução");
  23.       }
  24.     } else if (os.matches("Windows(.*)")) {
  25.  
  26.       try {
  27.         comando = "copy NUL " + System.getProperty("user.home") + "\\Documents\\cadastrosAlms.txt";
  28.         run.exec(comando);
  29.         System.out.println("Arquivo \"cadastrosAlms.txt\" criado em " + System.getProperty("user.home") + "\\Documentos");
  30.       } catch (IOException ex) {
  31.         System.out.println("Não foi possível criar o arquivo");
  32.         System.out.println("\terro: " + ex);
  33.       }
  34.       finally {
  35.         System.out.println("Encerrando a execução");
  36.       }
  37.  
  38.     } else {
  39.  
  40.       System.out.println("Sistema operacional não suportado pelo programa");
  41.  
  42.     }
  43.        
  44.   }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement