Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Entregable9_1;
- import java.io.*;
- import java.util.Scanner;
- /**
- *
- * @author unaim
- */
- public class DescifradoCesarSinClave {
- public static void main(String[] args) {
- int fin = 0;
- char[] x;
- int[] veces = new int[26];
- Scanner scFile = null;
- int n = 0;
- File fichero = null;
- File fichero1 = null;
- try {
- fichero = new File(args[0]);
- fichero1 = new File(fichero.getName() + ".uncfr");
- scFile = new Scanner(fichero);
- while (scFile.hasNext()) {
- x = scFile.next().toCharArray();
- for (char ch : x) {
- if (Character.getNumericValue(ch) > 0) {
- veces[(Character.getNumericValue(ch) - 10)]++;
- }
- }
- }
- for (int i = 0; i < veces.length; i++) {
- if (veces[i] > fin) {
- fin = veces[i];
- n = i;
- }
- }
- } catch (Exception e) {
- System.out.println("Ha ocurrido un error");
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- }
- scFile = null;
- FileWriter fw = null;
- String salida;
- int distancia;
- String[] linea;
- try {
- scFile = new Scanner(fichero);
- fw = new FileWriter(fichero1);
- distancia = n - 4;
- while (scFile.hasNextLine()) {
- linea = scFile.nextLine().split(" ");
- for (String palabra : linea) {
- salida = MetodoCifrar.cifrarFichero(distancia * -1, palabra);
- fw.write(salida + " ");
- }
- fw.write("\n");
- }
- System.out.println("el archivo se ha descifrado correctamente");
- } catch (Exception e) {
- System.out.println("Ha ocurrido un error");
- } finally {
- if (fw != null) {
- try {
- fw.close();
- } catch (Exception e) {
- System.out.println("ha ocurrido un error");
- }
- }
- if (scFile != null) {
- scFile.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement