Created
November 28, 2010 21:20
-
-
Save lfborjas/719311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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