Java - práce se soubory
Napsal: 13 kvě 2013 17:04
Ahoj, dělám ročníkovku a znějákého důvodu se mi soubory nevytvářejí a nenačítají. Prosím o radu :(
Kód: Vybrat vše
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
char cipher, code;
boolean again;
do {
do {
System.out.println("Vyberte šifru");
System.out.println("1 - Caesarova šifra");
System.out.println("2 - Railfence");
cipher = scanner.next().trim().charAt(0);
again = cipher != '1' && cipher != '2';
if (again) {
System.out.println("Zadejte prosím možnost dle výběru");
}
} while (again);
System.out.println();
do {
System.out.println("Vyberte činnost");
System.out.println("1 - Zašifrovat");
System.out.println("2 - Dešifrovat");
code = scanner.next().trim().charAt(0);
again = code != '1' && code != '2';
if (again) {
System.out.println("Zadejte prosím možnost dle výběru");
}
} while (again);
System.out.println();
String temp;
do {
System.out.print("Zadejte ");
if (cipher == '1') {
System.out.println("velikost posunu");
} else {
System.out.println("počet kolejí");
}
temp = scanner.next().trim();
again = false;
for (char ch : temp.toCharArray()) {
if (!Character.isDigit(ch)) {
again = true;
break;
}
}
if (again) {
System.out.println("Zadejte prosím celočíselnou hodnotu");
}
} while (again);
int number = Integer.valueOf(temp).intValue();
//Nesahat na to, co je nahoře!
char inputF;
do {
System.out.print("Chcete ");
if (code == '1') {
System.out.print("zprávu");
} else {
System.out.print("šifru");
}
System.out.println(" zadat, nebo načíst ze souboru?");
System.out.println("1 - Zadat");
System.out.println("2 - Načíst");
inputF = scanner.next().trim().charAt(0);
again = inputF != '1' && inputF != '2';
if (again) {
System.out.println("Zadejte správnou hodnotu");
}
} while (again);
String string = "";
if (inputF == '1') {
System.out.println();
System.out.print("Zadejte ");
if (code == '1') {
System.out.println("zprávu");
} else {
System.out.println("šifru");
}
string = scanner.next();
} else {
//Tady bude to načtení souboru do stringu "string".
String nacteno = "";
try {
System.out.println("Umístěte soubor do složky, ve které se nachází šifrix a napište jeho název (Např.: sifra.txt)");
String fileName = scanner.next();
Scanner soubor = new Scanner(new File(fileName));
while (soubor.hasNext()) {
String line = soubor.nextLine();
nacteno = nacteno + line;
}
} catch (Exception e) {
System.out.println("Chyba při otevírání souboru");
nacteno = "Nefunguje to";
}
System.out.println(nacteno);
}
if (code == '1') {
if (cipher == '1') {
string = caesarEncode(string, number);
} else {
string = railFenceEncode(string, number);
}
} else {
if (cipher == '1') {
string = caesarDecode(string, number);
} else {
string = railFenceDecode(string, number);
}
}
System.out.println();
System.out.println("Výsledek: ");
System.out.println(string);
//Ukládání
char save;
do {
System.out.println("Přejete si šifru uložit do souboru?");
System.out.println("1 - Ano");
System.out.println("2 - Ne");
save = scanner.next().trim().charAt(0);
again = save != '1' && save != '2';
if (again) {
System.out.println("Zadej to správně");
}
} while (again);
if (save == '1') {
System.out.println("Výsledek bude uložen do souboru vysledek.txt");
try {
PrintWriter zapsat = new PrintWriter(new FileWriter("vysledek.txt"));
zapsat.println(string);
zapsat.close();
} catch (Exception e) {
//Catch exception if any
System.err.println("Chyba: " + e.getMessage());
}
} else {
System.out.println("Fajn. Nemusíme ukládat.");
}
//konec ukládání
char repeat;
do {
System.out.println("Znovu?");
System.out.println("1 - Ano");
System.out.println("2 - Ne");
repeat = scanner.next().trim().charAt(0);
again = repeat != '1' && repeat != '2';
if (again) {
System.out.println("Zadejte prosím možnost dle výběru");
}
} while (again);
again = repeat == '1';
System.out.println();
} while (again);
System.exit(0);
}