/** * Compress. Using huffman table in args[0] compress the file in args[1] * into the file in args[2] * * @author rpj * @version 22xi08 */ import java.util.*; import java.io.*; public class Compress { /** * main * read the file whose name is provided on commandline */ public static void main (String[] args) { Scanner scan = null; try { scan = new Scanner(new File(args[0])); } catch (Exception fnfe) { System.err.println("Usage: java Compress tablefile tocompressfile compressedfile"); System.exit(-1); } String cs; // Read the huffman code table into a HashMap for easy encoding HashMap huffmanTable = new HashMap(); while (scan.hasNextLine()) { cs = scan.nextLine(); // each line in a huffman table begins with the character // followed by " : " // and then the 0-1 encoding // charAt(0) gives the character, and substring(4) gives the code huffmanTable.put(cs.charAt(0), cs.substring(4)); } try { scan = new Scanner(new File(args[1])); } catch (Exception fnfe) { // handle this oops! } PrintStream outFile = null; try { outFile = new PrintStream(new File (args[2])); } catch (Exception e) { // handle this oops! } while (scan.hasNextLine()) { // one line at a time cs = scan.nextLine(); // output the 0-1 code got by looking up each char in turn: for (int i = 0; i