fork download
  1. /**Driver that allows running of algorithms on text input
  2.   *
  3.   * Samantha Fitzpatrick
  4.   */
  5.  
  6.  
  7. import java.util.Scanner;
  8. import java.io.File;
  9. import java.io.PrintWriter;
  10. import java.io.FileWriter;
  11. import java.io.FileOutputStream;
  12. import java.io.FileInputStream;
  13. import java.io.OutputStream;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.regex.Pattern;
  17. import java.util.regex.Matcher;
  18.  
  19. public class Driver_jgrasp{
  20.  
  21. /**Main driver to open files
  22.   *
  23.   *@param args[] Holds command line arguments: filename.
  24.   */
  25. public static void main(String[] args) throws Exception {
  26. // Variables to hold all command-line inputs and switches.
  27. String inputFile = "";
  28. String outputFile = "";
  29. Boolean readAsBytes = false;
  30. Boolean writeAsBytes = false;
  31.  
  32. // input and output text Lists
  33. ArrayList<String> text = new ArrayList<String>();
  34. ArrayList<String> outText = new ArrayList<String>();
  35.  
  36. // regex pattern match for command-line arguments
  37. Pattern p = Pattern.compile("(^-{1,2}[a-zA-Z]+)[0-9]*$");
  38.  
  39. // Loop through command-line arguments
  40. for (int i=0; i<args.length; i++) {
  41. Matcher m = p.matcher(args[i]);
  42.  
  43. String inputArg = "";
  44. if (m.find()) {
  45. inputArg = m.group(1);
  46. }
  47.  
  48. // Handle each found Argument. Only arguments starting with -- or -
  49. // will be parsed as arguments. Everything else will only be parsed
  50. // if there is a rule to take in the value with a given option.
  51. switch (inputArg) {
  52. case "--help":
  53. case "-h":
  54. System.out.println("Usage: ");
  55. System.out.println("--help, -h: Print this helpfile and exit.");
  56. System.out.println("--input, -i: Filename to read input text from.");
  57. System.out.println("--readbytes, -rb: Read input as binary.");
  58. System.out.println("--output, -o: Filename to send output to. If omitted, STDOUT will be used.");
  59. System.out.println("--writebytes, -wb: Write output as binary.");
  60. System.out.println("--compress, -c: Flag to run compression.");
  61. System.out.println(" -c huffman: to run huffman compression");
  62. System.out.println(" -c lzw: to run LZW compression.");
  63. System.out.println("--huffmanfiles: Files from which to build huffman tree.");
  64. System.out.println(" only used if huffman compression is run.");
  65. System.out.println("--extract, -x: Flag to decompress the text.");
  66. System.out.println(" -x huffman: decompress with Huffman");
  67. System.out.println(" -x lzw: decompress with LZW.");
  68. System.out.println("--encrypt, -e: Flag to encrypt the text.");
  69. System.out.println("--decrypt, -d: Flag to decrypt the text.");
  70. return;
  71.  
  72. // File to take input text from
  73. case "--input":
  74. case "-i":
  75. inputFile = args[i+1];
  76. break;
  77.  
  78. // Flag to read input as binary instead of ascii
  79. case "--readbytes":
  80. case "-rb":
  81. readAsBytes = true;
  82. break;
  83.  
  84. // File to output changed text to
  85. case "--output":
  86. case "-o":
  87. outputFile = args[i+1];
  88. break;
  89.  
  90. // Flag to write output as binary instead of ascii
  91. case "--writebytes":
  92. case "-wb":
  93. writeAsBytes = true;
  94. break;
  95.  
  96. // Flag to compress input
  97. // Will parse the following argument to specify compression algorithm
  98. case "--compress":
  99. case "-c":
  100. break;
  101.  
  102. // Specify input text files to build huffman tree.
  103. // all strings following argument until next optional parameter is found
  104. // specified by string starting with '-'.
  105. case "--huffmanfiles":
  106. break;
  107.  
  108. // Extract (decompress) input
  109. // Immediately following argument will specify algorithm to use
  110. case "--extract":
  111. case "-x":
  112. break;
  113.  
  114. // Flag to encrypt output before writing.
  115. // Following argument will specify algorithm to use
  116. case "--encrypt":
  117. case "-e":
  118. break;
  119.  
  120. // Flag to decrypt output after reading.
  121. // Following argument will specify algorithm to use
  122. case "--decrypt":
  123. case "-d":
  124. break;
  125.  
  126. // File to specify keys for encryption algorithms
  127. case "--keyfile":
  128. case "-k":
  129. break;
  130. }
  131. }
  132.  
  133.  
  134. // Read text from input file and store in ArrayList
  135. if (inputFile != "") {
  136. File buffer = new File(inputFile);
  137. if (buffer.exists() && !buffer.isDirectory()) {
  138. if (readAsBytes) {
  139. text = readFileInputStream(inputFile);
  140. } else {
  141. text = ReadFile(inputFile);
  142. }
  143. } else {
  144. System.out.println("Input file not found on disk: " + inputFile);
  145. }
  146. }
  147. // Compute raw size of text input
  148. int rawsize = 0;
  149. for (String s : text) {
  150. rawsize = rawsize + s.length();
  151. }
  152.  
  153. // Compute size of text output
  154. int outsize = 0;
  155. for (String s : outText) {
  156. outsize = outsize + s.length();
  157. }
  158.  
  159. System.out.println("Raw character length: " + rawsize);
  160.  
  161. //Output information: either to stdout or a filename
  162. if (outputFile != "") {
  163. System.out.println("#-----------------------#");
  164. System.out.println("Writing file " + outputFile);
  165. System.out.println("#-----------------------#");
  166. if (writeAsBytes) {
  167. WriteToFile(outText, outputFile, writeAsBytes);
  168. } else {
  169. WriteToFile(outText, outputFile);
  170. }
  171. } else {
  172. System.out.println("No output file supplied, redirecting to STDOUT");
  173. for (int i=0; i<outText.size(); i++) {
  174. if (writeAsBytes) {
  175. System.out.println(outText.get(i).getBytes());
  176. } else {
  177. System.out.println(outText.get(i));
  178. }
  179. }
  180. }
  181.  
  182. }
  183.  
  184.  
  185. /** Read from input File
  186.   */
  187. private static ArrayList ReadFile(String filename) throws java.io.FileNotFoundException {
  188. Scanner scanner = new Scanner(new File(filename));
  189.  
  190. ArrayList<String> words = new ArrayList<String>();
  191.  
  192. System.out.println("#-----------------------#");
  193. System.out.println("Reading file " + filename);
  194. System.out.println("#-----------------------#");
  195.  
  196. while(scanner.hasNextLine()){
  197. words.add(scanner.nextLine());
  198. }
  199.  
  200. return words;
  201. }
  202.  
  203. /** Read from input file as an
  204.   */
  205.  
  206. private static ArrayList readFileInputStream(String filename) throws IOException {
  207. String sContent=null;
  208. byte[] buffer =null;
  209. File a_file = new File(filename);
  210.  
  211. try {
  212. FileInputStream fis = new FileInputStream(filename);
  213. int length = (int)a_file.length();
  214. buffer = new byte [length];
  215. fis.read(buffer);
  216. fis.close();
  217. } catch(IOException e) {
  218. e.printStackTrace();
  219. }
  220.  
  221. sContent = new String(buffer);
  222.  
  223. ArrayList<String> text = new ArrayList<String>();
  224. text.add(sContent);
  225.  
  226. return text;
  227. }
  228.  
  229. /** Write to file as ascii
  230.   */
  231. private static void WriteToFile(ArrayList text, String outName) {
  232. PrintWriter pw = null;
  233.  
  234. try {
  235. File file = new File(outName);
  236. FileWriter fw = new FileWriter(file, true);
  237. pw = new PrintWriter(fw);
  238.  
  239. for (int i=0; i<text.size(); i++) {
  240. pw.println(text.get(i));
  241. }
  242.  
  243. } catch (IOException e) {
  244. e.printStackTrace();
  245. } finally {
  246. if (pw != null) {
  247. pw.close();
  248. }
  249. }
  250. }
  251.  
  252. /** Write to file as binary
  253.   */
  254. private static void WriteToFile(ArrayList<String> text, String outName, Boolean bytes) {
  255. OutputStream output = null;
  256.  
  257. try {
  258. File file = new File(outName);
  259. output = new FileOutputStream(outName);
  260.  
  261. for (int i=0; i<text.size(); i++) {
  262. byte[] outBytes = text.get(i).getBytes();
  263. output.write(outBytes);
  264. }
  265.  
  266. } catch (IOException e) {
  267. e.printStackTrace();
  268. } finally {
  269. try{
  270. if (output != null) {
  271. output.close();
  272. }
  273. } catch (IOException e) {
  274. e.printStackTrace();
  275. }
  276. }
  277. }
  278. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Sally sells seashells by the seashore
Peter Piper picked a peck of pickled peppers a peck of pickled peppers Peter Piper picked
Houston the Eagle has landed
Is that your final answer
compilation info
Main.java:19: error: class Driver_jgrasp is public, should be declared in a file named Driver_jgrasp.java
public class Driver_jgrasp{
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty