Question: I need this code to be able to read a big pdf file and encode it in HEX import java.util.Scanner; import java.io.PrintStream; import java.io.IOException; import
I need this code to be able to read a big pdf file and encode it in HEX import java.util.Scanner; import java.io.PrintStream; import java.io.IOException; import java.io.File; public class IOExample4{ //applied function from First IO Exercise public static String toHex(String currString){ //converts a string to a byte-array byte[] preConvert = currString.getBytes(); //converts byte-array to hexadecimal String using a function from the javax library return javax.xml.bind.DatatypeConverter.printHexBinary(preConvert); } //applied function from Second IO Exercise public static String printDec(String hexString){ int num1 = Integer.parseInt(hexString, 16); String current = ""; current += (char) num1; return current; } //applied function from Second IO Exercise public static String takeHexLine(String hexLine){ String finalString = ""; String[] hexArray = hexLine.split("(?<=\\G..)"); for(int i = 0; i < hexArray.length; i++){ finalString += printDec(hexArray[i]); } return finalString; } public static void main(String[] args) throws IOException{ //String[] args used in the declaration of the main method represents flags given at program runtime //numArgs represents the number of flags given at runtime int numArgs = args.length; //if no arguments are supplied, print out an error that will lead the user to the help output if(numArgs == 0){ System.out.println("ERROR: No input given. Run with -help for more information."); } else{ //if arguments are supplied, use a switch statement to determine what processes to run switch(args[0]){ //if "java IOExample4 -help" is run, print out a display of information that informs the user of the multiple ways the program can be run case "-help": System.out.println("-r \"filename\"\t\t\t\t Allows you to output a file written in hexidecimal to UTF-8."); System.out.println("-rf \"inputFilename\" \"outputFilename\"\t Reads a hexidecimal file and outputs it in UTF-8 with the specified filename."); System.out.println("-w \"filename\" \"String\"\t\t\t Allows you to write a UTF-8 String to a file in hexidecimal."); System.out.println("-help\t\t\t\t\t Brings up this information."); break; //if "java IOExample4 -r "filename"" is supplied, print the contents of a hexdump to the command-line in plain-text case "-r": //applied function from Exercise 52 Scanner currFile = new Scanner(new File(args[1])); String finalString = takeHexLine(currFile.nextLine()); System.out.println(finalString); currFile.close(); break; //if "java IOExample4 -rf "inputFilename" "outputFilename"" is supplied, print the contents of a hexdump to a specified file in plaintext case "-rf": //applied function from Exercise 52, and concepts from Exercise 50 Scanner currFile1 = new Scanner(new File(args[1])); String finalString1 = takeHexLine(currFile1.nextLine()); PrintStream fileStream = new PrintStream(args[2]); fileStream.println(finalString1); fileStream.close(); currFile1.close(); break; //if "java IOExample4 -w "filename" "String to dump"" is supplied, then write the string to the specified file as a hexdump case "-w": //applied concept from Exercise 50 String hexString = toHex(args[2]); PrintStream fileStream1 = new PrintStream(args[1]); fileStream1.println(hexString); fileStream1.close(); break; //if invalid arguments are supplied, print out an error message that suggests the user run with the -help flag default: System.out.println("ERROR: Invalid parameters. Run with -help for more information."); } } } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
