Advertisement
Carlos98

Utils

Mar 31st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.util.Scanner;
  4.  
  5. public class Utils {
  6.  
  7.     final static String alfabeto = "abcdefghijklmnopqrstuvwxyz";
  8.     final static String alfabetoMayuscula = alfabeto.toUpperCase();
  9.  
  10.     public static String cifrar(String texto, int numero) {
  11.         String resuelto = "";
  12.         char letra;
  13.         int posicion;
  14.        
  15.         while(numero<0){
  16.             numero = 26 + numero;
  17.         }
  18.        
  19.         for (int i = 0; i < texto.length(); i++) {
  20.             letra = texto.charAt(i);
  21.             if (Character.isLowerCase(letra)) {
  22.                 posicion = alfabeto.indexOf(letra);
  23.                 if (posicion >= 0) {
  24.                     letra = alfabeto.charAt(Math.abs((posicion + numero) % alfabeto.length()));
  25.                 }
  26.             } else {
  27.                 posicion = alfabetoMayuscula.indexOf(letra);
  28.                 if (posicion >= 0) {
  29.                     letra = alfabetoMayuscula.charAt(Math.abs((posicion + numero) % alfabetoMayuscula.length()));
  30.                 }
  31.             }
  32.             resuelto += letra;
  33.         }
  34.         return resuelto;
  35.     }
  36.  
  37.     public static boolean escribirTexto(String ruta, String rutaResultado, int desplazamiento) {
  38.  
  39.         File archivo = null;
  40.         Scanner scFile = null;
  41.         File archivoResuelto = null;
  42.         FileWriter fw = null;
  43.         boolean hechoCorrectamente = false;
  44.  
  45.         try {
  46.  
  47.             archivo = new File(ruta);
  48.  
  49.             archivoResuelto = new File(rutaResultado);
  50.  
  51.             fw = new FileWriter(archivoResuelto);
  52.  
  53.             scFile = new Scanner(archivo);
  54.  
  55.             while (scFile.hasNextLine()) {
  56.                 fw.write(Utils.cifrar(scFile.nextLine(), desplazamiento) + "\n");
  57.             }
  58.  
  59.             hechoCorrectamente = true;
  60.         } catch (Exception e) {
  61.             System.out.println(e.getMessage());
  62.             e.printStackTrace();
  63.             hechoCorrectamente = false;
  64.         } finally {
  65.             if (!scFile.equals(null)) {
  66.                 try {
  67.                     scFile.close();
  68.                 } catch (Exception e) {
  69.                     System.out.println(e.getMessage());
  70.                     e.printStackTrace();
  71.                     System.out.println("Error al cerrar");
  72.                     hechoCorrectamente = false;
  73.                 }
  74.             }
  75.             if (!fw.equals(null)) {
  76.                 try {
  77.                     fw.close();
  78.                 } catch (Exception e) {
  79.                     System.out.println(e.getMessage());
  80.                     e.printStackTrace();
  81.                     System.out.println("Error al cerrar");
  82.                     hechoCorrectamente = false;
  83.                 }
  84.             }
  85.         }
  86.         return hechoCorrectamente;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement