Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.util.Scanner;
- public class Utils {
- final static String alfabeto = "abcdefghijklmnopqrstuvwxyz";
- final static String alfabetoMayuscula = alfabeto.toUpperCase();
- public static String cifrar(String texto, int numero) {
- String resuelto = "";
- char letra;
- int posicion;
- while(numero<0){
- numero = 26 + numero;
- }
- for (int i = 0; i < texto.length(); i++) {
- letra = texto.charAt(i);
- if (Character.isLowerCase(letra)) {
- posicion = alfabeto.indexOf(letra);
- if (posicion >= 0) {
- letra = alfabeto.charAt(Math.abs((posicion + numero) % alfabeto.length()));
- }
- } else {
- posicion = alfabetoMayuscula.indexOf(letra);
- if (posicion >= 0) {
- letra = alfabetoMayuscula.charAt(Math.abs((posicion + numero) % alfabetoMayuscula.length()));
- }
- }
- resuelto += letra;
- }
- return resuelto;
- }
- public static boolean escribirTexto(String ruta, String rutaResultado, int desplazamiento) {
- File archivo = null;
- Scanner scFile = null;
- File archivoResuelto = null;
- FileWriter fw = null;
- boolean hechoCorrectamente = false;
- try {
- archivo = new File(ruta);
- archivoResuelto = new File(rutaResultado);
- fw = new FileWriter(archivoResuelto);
- scFile = new Scanner(archivo);
- while (scFile.hasNextLine()) {
- fw.write(Utils.cifrar(scFile.nextLine(), desplazamiento) + "\n");
- }
- hechoCorrectamente = true;
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- hechoCorrectamente = false;
- } finally {
- if (!scFile.equals(null)) {
- try {
- scFile.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- System.out.println("Error al cerrar");
- hechoCorrectamente = false;
- }
- }
- if (!fw.equals(null)) {
- try {
- fw.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- System.out.println("Error al cerrar");
- hechoCorrectamente = false;
- }
- }
- }
- return hechoCorrectamente;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement