Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Entregable;
- import java.io.File;
- import java.io.FileWriter;
- import java.util.HashMap;
- import java.util.Scanner;
- /**
- *
- * @author javie
- */
- public class DescifradoSinDesplazamiento {
- public static void main(String[] args) {
- HashMap<String, Integer> letras = new HashMap<String, Integer>();
- FileWriter fw = null;
- Scanner scFile = null;
- String nombreFichero = args[0];
- int des;
- String[] linea;
- char[] fila;
- String texto = "";
- String LetraE = "a";
- int CantidadLetraE = 0;
- File f = new File(nombreFichero);
- File fe = new File("./" + nombreFichero + ".uncfr");
- if (!f.exists()) {
- System.out.println("El fichero no existe");
- } else {
- try {
- scFile = new Scanner(f);
- fw = new FileWriter(fe);
- while (scFile.hasNextLine()) {
- linea = scFile.nextLine().split("");
- for (String letra : linea) {
- letra = letra.toLowerCase();
- if (cifrado.letra(letra)) {
- if (letras.containsKey(letra)) {
- letras.put(letra, letras.get(letra) + 1);
- } else {
- letras.put(letra, 0);
- }
- }
- }
- }
- for (String c : letras.keySet()) {
- if (letras.get(c) > CantidadLetraE) {
- CantidadLetraE = letras.get(c);
- LetraE = c;
- }
- }
- scFile = new Scanner(f);
- while (scFile.hasNextLine()) {
- fila = scFile.nextLine().toCharArray();
- for (char letra : fila) {
- texto = texto + cifrado.cifrar(letra, cifrado.desplazamiento(LetraE), true);
- }
- fw.write(texto + "\n");
- texto = "";
- }
- System.out.println("Fichero descifrado");
- } catch (Exception e) {
- System.out.println("Se ha producido un error");
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- if (fw != null) {
- try {
- fw.close();
- } catch (Exception e) {
- System.out.println("Error al cerrar");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement