Advertisement
Unaimartin

DescifradoCesarSinClave

Mar 31st, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 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 DescifradoCesarSinClave {
  11.  
  12. public static void main(String[] args) {
  13. int fin = 0;
  14. char[] x;
  15. int[] veces = new int[26];
  16. Scanner scFile = null;
  17. int n = 0;
  18. File fichero = null;
  19. File fichero1 = null;
  20. try {
  21. fichero = new File(args[0]);
  22. fichero1 = new File(fichero.getName() + ".uncfr");
  23. scFile = new Scanner(fichero);
  24. while (scFile.hasNext()) {
  25. x = scFile.next().toCharArray();
  26. for (char ch : x) {
  27. if (Character.getNumericValue(ch) > 0) {
  28. veces[(Character.getNumericValue(ch) - 10)]++;
  29. }
  30. }
  31. }
  32. for (int i = 0; i < veces.length; i++) {
  33. if (veces[i] > fin) {
  34. fin = veces[i];
  35. n = i;
  36. }
  37. }
  38. } catch (Exception e) {
  39. System.out.println("Ha ocurrido un error");
  40. } finally {
  41. if (scFile != null) {
  42. scFile.close();
  43. }
  44. }
  45. scFile = null;
  46. FileWriter fw = null;
  47. String salida;
  48. int distancia;
  49. String[] linea;
  50. try {
  51. scFile = new Scanner(fichero);
  52. fw = new FileWriter(fichero1);
  53. distancia = n - 4;
  54. while (scFile.hasNextLine()) {
  55. linea = scFile.nextLine().split(" ");
  56. for (String palabra : linea) {
  57. salida = MetodoCifrar.cifrarFichero(distancia * -1, palabra);
  58. fw.write(salida + " ");
  59. }
  60. fw.write("\n");
  61. }
  62. System.out.println("el archivo se ha descifrado correctamente");
  63. } catch (Exception e) {
  64. System.out.println("Ha ocurrido un error");
  65. } finally {
  66. if (fw != null) {
  67. try {
  68. fw.close();
  69. } catch (Exception e) {
  70. System.out.println("ha ocurrido un error");
  71. }
  72. }
  73. if (scFile != null) {
  74. scFile.close();
  75. }
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement