I am pretty new to Java
, kind of getting the hang of it, and am working on some code for a Caesar Cipher in my CS class. I have it able to read one line of code from a file and it can decrypt
and encrypt
just fine, but I think my teacher wants a full document to be able to be encrypted or decrypted. I know it's possible to have it read the entire document and store it into a string, but how would I go about doing that?
Thanks for any help! Let me know if you need to see my other Class, if you need it for reference.
import java.util.Scanner;
import java.io.*;
public class Project3 {
public static void main(String[] args) {
Cipher caesar = new Cipher();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a filename to encode or decode.");
String f = input.nextLine();
String fileName = f;
String line = null;
System.out.println("Enter the number of steps to encode or decode the file by: ");
int i = input.nextInt();
input.nextLine();
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
System.out.println("What would you like to do? Enter [e] to encrypt or press [d] to decrypt.");
String a = input.nextLine();
if(a.equals("e")){
String encoded = caesar.useCipher(line,i);
caesar.writeToFile(encoded);
break;
}
if(a.equals("d")){
String decoded = caesar.useCipher(line,(-i));
caesar.writeToFile(decoded);
break;
}
}
}
catch(FileNotFoundException ex){
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex){
System.out.println("Error reading file '" + fileName + "'");
}
}
}