Advertisement
Unaimartin

DescifradoCesar

Mar 31st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package Entregable9_1;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. *
  8. * @author unaim
  9. */
  10. public class DescifradoCesar {
  11. public static void main(String[] args) {
  12. Scanner scFile = null;
  13. FileWriter fw = null;
  14. String[] linea;
  15. String salida;
  16. int n;
  17. try {
  18. File fichero = new File(args[0]);
  19. File fichero1 = new File(fichero.getName() + ".uncfr");
  20. scFile = new Scanner(fichero);
  21. fw = new FileWriter(fichero1);
  22. if (Integer.parseInt(args[1]) > 25) {
  23. n = Integer.parseInt(args[1]) % 26;
  24. } else if (Integer.parseInt(args[1]) < -25) {
  25. n = Integer.parseInt(args[1]) * -1 % 26;
  26. n *= -1;
  27. }else{
  28. n = Integer.parseInt(args[1]);
  29. }
  30. while (scFile.hasNextLine()) {
  31. linea = scFile.nextLine().split(" ");
  32. for (String palabra : linea) {
  33. salida = MetodoCifrar.cifrarFichero(n * -1, palabra);
  34. fw.write(salida + " ");
  35. }
  36. fw.write("\n");
  37. }
  38. System.out.println("El archivo ha sido descifrado correctamente");
  39. } catch (Exception e) {
  40. System.out.println("Se ha producido un error");
  41. }finally{
  42. if(scFile != null){
  43. scFile.close();
  44. }
  45. if (fw != null) {
  46. try{
  47. fw.close();
  48. }catch(Exception e){
  49. System.out.println("Se ha producido un error");
  50. }
  51. }
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement