Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Created November 28, 2010 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfborjas/719311 to your computer and use it in GitHub Desktop.
Save lfborjas/719311 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Enigma{
public static String readEnigma(String path) throws Exception{
StringBuilder letras = new StringBuilder();
//abrir el archivo, podría no encontrarlo
FileInputStream in = new FileInputStream(path);
int c = 0;
//leer hasta consumir el flujo
while( (c = in.read() ) != -1){
if(c > 65)
letras.append((char)(c-13));
else
letras.append((char)c);
}
//siempre cerrar ! Si no, el flujo se corrompe
in.close();
return letras.toString();
}
public static void writeEnigma(String enigma, String destination) throws Exception{
FileOutputStream out = null;
out = new FileOutputStream(destination);
for(char c : enigma.toCharArray()){
if(((int)c) < 65)
out.write(c);
else
out.write(c+13);
}
out.close();
}
public static void main (String [] args) throws Exception
{
if(args.length < 2 && args.length == 1)
System.out.printf("La solución al enigma es: %s \n", readEnigma(args[0]) );
else{
writeEnigma(args[0], args[1]);
System.out.printf("Escribiendo el enigma %s en %s \n", args[0], args[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment