This means you will need to ask the user the direction to shift (left or right) and the number of places to shift (should they choose to encrypt a file). The number of places a file can be shifted is anywhere from 0 to 2 billion. You may assume the input from the user will be in this range (the user will NOT enter a negative number). If the user chooses to encrypt, present them with a menu that finds out which direction to shift for encryption (left or right). When encrypting a file, before you write the encrypted data to an output file, you'll need to get the name of the file to encrypt and what to call the encrypted file from the user. The first thing in the file you build that is encrypted is the direction of the shift ('left' or 'right' -- use these words), followed by a space, followed by the number of spaces shifted. This information will be used by the decrypt portion of this program. Finally, you can proceed to write the encrypted data to the file.
To decrypt a file, get the name of the file to decrypt from the user and the name the user wants to call the decrypted file. Use the direction and places found on the first line in the encrypted file to determine how to decrypt. You may assume the first line of the file will contain the direction of shift followed by the number of places shifted. You may also assume the file to decrypt exists. Be sure NOT to include the shift direction and number of spaces in the decrypted file!
NOTE: once a file has been encrypted or decrypted, be sure and close that file!
You will/may need a total of three (3) files for this project: the original input file, the file you created from encrypting, and the file created from decrypting. Depending on what the user chooses to do, you may not ever create an encrypted and decrypted file. Incidentally, the decrypted file should end up looking the same as the original file...
The only output to the monitor in this program is: (1) the menu requesting input to encrypt, decrypt, or quit (2) request for the name of an input file, name to call encrypted file, and the direction of shift and number of places if encrypt is chosen (3) request for name of the encrypted file and what to call the decrypted file if decrypt is chosen (4) error messages resulting from a file not opening.
Expand|Select|Wrap|Line Numbers
- import java.io.*;//file, fileNotFoundException, printStream
- import java.util.Scanner;
- public class FileEncrypter
- {
- public static void main(String [] args) throws FileNotFoundException
- {
- int ShiftValue;
- Scanner inputFile;
- PrintStream outputFile;
- Scanner keyboard = new Scanner(System.in);
- inputFile = openInputFile(keyboard);
- outputFile = openOutputFile(keyboard);
- ShiftValue = getShiftValue(keyboard);
- getShiftValue(keyboard);
- EncryptFile (inputFile, outputFile, ShiftValue);
- inputFile.close();
- outputFile.close();
- }//end of main
- public static int getShiftValue(Scanner keyboard)
- {
- String directionShift = keyboard.next();
- int positionShift = keyboard.nextInt();
- int ShiftValue = 0;
- System.out.print("Enter the direction you would like to shift and the number of positions: ");
- // directionShift = keyboard.next();
- // positionShift = keyboard.nextInt();
- while(ShiftValue == positionShift)
- {
- if(directionShift.equalsIgnoreCase("left"))
- {
- ShiftValue = positionShift * 1;
- }
- else if(directionShift.equalsIgnoreCase("right"))
- {
- ShiftValue = positionShift * (-1);
- }
- }//end while
- return ShiftValue;
- }
- public static Scanner openInputFile(Scanner keyboard) throws FileNotFoundException
- {
- Scanner inputFile;
- String fileName;
- System.out.print("Enter name of input file: ");
- fileName = keyboard.nextLine();
- inputFile = new Scanner(new File(fileName));
- return inputFile;
- }//end method InputFile
- public static void EncryptFile(Scanner inputFile, PrintStream outputFile, int ShiftValue)
- {
- String line;
- ShiftValue = ShiftValue%26;
- while (inputFile.hasNextLine())
- {
- line = inputFile.nextLine();
- for (int i = 0; i < line.length(); i++)
- {
- char ch = line.charAt(i);
- ch = ch + ShiftValue;
- if (ch > 'Z' || ch > 'z')
- {
- ch = ch -26;
- System.out.print(ch);
- outputFile.print(ch);
- }
- else if (ch < 'A' || ch < 'a')
- {
- ch = ch + 26;
- System.out.print(ch);
- outputFile.print(ch);
- }
- }//end for loop
- System.out.println();//finished processing a line
- outputFile.println();
- }//end while
- }//end EncryptFile
- public static PrintStream openOutputFile(Scanner keyboard) throws FileNotFoundException
- {
- String fileName;
- PrintStream outFile;
- System.out.print("Enter name of output file: ");
- fileName = keyboard.nextLine();
- outFile = new PrintStream(fileName);
- return outFile;
- }//end of OutputFile
- }//end of class