Javier_Fernandez

CifradoCesar

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