Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package entregable01;
  2.  
  3. import com.entregable.exceptions.LongKeyException;
  4. import com.entregable.utils.Utils;
  5. import java.io.*;
  6. import java.util.Scanner;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. /**
  11. *
  12. * @author jairo
  13. */
  14. public class CifrarFichero {
  15.  
  16. public static void main(String[] args) {
  17. Scanner in = new Scanner(System.in);
  18. String nombre;
  19. String linea;
  20. String salida = "";
  21. int codigo;
  22.  
  23. if (args.length == 0) {
  24. nombre = "./test/Ejemplo.txt";
  25. codigo = -26;
  26.  
  27. } else {
  28. nombre = args[0];
  29. codigo = Integer.parseInt(args[1]);
  30.  
  31. }
  32. // LongKey(codigo);
  33. File f = new File(nombre);
  34. if (!f.exists()) {
  35. System.out.println("El fichero no existe");
  36. } else {
  37. Scanner inFile = null;
  38. File fi = null;
  39. FileWriter fw = null;
  40. try {
  41. fi = new File(nombre + ".cfr");
  42. inFile = new Scanner(f);
  43. fw = new FileWriter(fi);
  44. while (inFile.hasNextLine()) {
  45. linea = inFile.nextLine();
  46. String cifrar = Utils.cifrar(linea, codigo);
  47. salida = salida + cifrar + "\n";
  48. }
  49. fw.write(salida);
  50. }catch (LongKeyException LO){
  51. System.out.println(LO.getMessage());
  52. } catch (Exception e) {
  53. System.out.println("Se ha producido un error");
  54. e.printStackTrace();
  55.  
  56. } finally {
  57. if (inFile != null) {
  58. inFile.close();
  59. }
  60. if (!fw.equals(null)) {
  61. try {
  62. fw.close();
  63. } catch (IOException ex) {
  64. System.out.println("Error al cerrar");
  65. }
  66. }
  67. }
  68. }
  69.  
  70. }
  71. // public static void LongKey(int codigo) {
  72. // if(codigo > 25 || codigo < -25){
  73. // try {
  74. // throw new LongKeyException();
  75. // } catch (LongKeyException ex) {
  76. // System.out.println(ex.getMessage());
  77. // }
  78. // }
  79. //
  80. // }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement