Encode the file by adding 5 to every byte in the file. Writer a program that pro
ID: 3546093 • Letter: E
Question
Encode the file by adding 5 to every byte in the file. Writer a program that prompts the user to enter an input file name and an output file name and saves the encrypted version of the input file to the output file.
This is what I came up with but it is not giving me the correct answer! Please Help!
import java.util.Scanner;
import java.io.*;
public class EncryptFile {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter an input file name: ");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(input.next())));
System.out.print("Enter an output file: ");
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(input.next())));
int value;
while((value = in.read()) != -1)
{
output.write(value + 5);
}
input.close();
output.close();
}
}
Explanation / Answer
This is indeed correct and is working. Have you opened the output file and checked on what you have in the file. You should see some Junk Characters in it.
Indeed to test this you have can a single character 'a' in the input file and you should a single character 'f' in your ouput file (Thats because you are adding '5' to 'a', which becomes 'f').
Please do rate.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.