Advertisement
Javier_Fernandez

DescifradoCesar

Mar 30th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package Entregable;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.util.Scanner;
  6. import Entregable.exception.TooHighNumberException;
  7.  
  8. /**
  9.  *
  10.  * @author javie
  11.  */
  12. public class DescifradoCesar {
  13.  
  14.     public static void main(String[] args) {
  15.         FileWriter fw = null;
  16.         Scanner scFile = null;
  17.         String nombreFichero = args[0];
  18.         int des = Integer.parseInt(args[1]);
  19.         char[] linea;
  20.         String texto = "";
  21.  
  22.         File f = new File(nombreFichero);
  23.         File fe = new File("./" + nombreFichero + ".uncfr");
  24.  
  25.         if (!f.exists()) {
  26.             System.out.println("El fichero no existe");
  27.         } else {
  28.             try {
  29.                
  30.                 scFile = new Scanner(f);
  31.                 fw = new FileWriter(fe);
  32.                 while (scFile.hasNextLine()) {
  33.                     linea = scFile.nextLine().toCharArray();
  34.                     for (char letra : linea) {
  35.                         texto = texto + cifrado.cifrar(letra, des, true);
  36.                     }
  37.                     fw.write(texto + "\n");
  38.                     texto = "";
  39.                 }
  40.                 System.out.println("Fichero descifrado");
  41.             } catch (TooHighNumberException e) {
  42.                 System.out.println("Desplazamiento fuera del limite");
  43.             } catch (Exception e) {
  44.                 System.out.println("Se ha producido un error");
  45.  
  46.             } finally {
  47.                 if (scFile != null) {
  48.                     scFile.close();
  49.                 }
  50.                 if (fw != null) {
  51.                     try {
  52.                         fw.close();
  53.                     } catch (Exception e) {
  54.                         System.out.println("Error al cerrar");
  55.                     }
  56.                 }
  57.             }
  58.  
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement