3 Replies Latest reply on May 7, 2007 5:38 AM by 807599

    where does this java program read a text file and how does it output the re

    807599
      someone sent this to me. its a generic translator where it reads a hashmap text file which has the replacement vocabulary etc... and then reads another text file that has what you want translated and then outputs translation. what i don't understand is where i need to plugin the name of the text files and what is the very first line of code?

      [code
      package forums;

      //references:
      //http://forums.techguy.org/development/570048-need-write-java-program-convert.html
      //http://www.wellho.net/resources/ex.php4?item=j714/Hmap.java

      import java.util.Map;
      import java.util.HashMap;
      import java.util.Iterator;

      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.io.BufferedReader;
      import java.io.FileReader;

      public class Translate {

      public static void main(String [] args) throws IOException {
      if (args.length != 2) {
      System.err.println("usage: Translate wordmapfile textfile");
      System.exit(1);
      }
      try {
      HashMap words = ReadHashMapFromFile(args[0]);
      System.out.println(ProcessFile(words, args[1]));
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      //----------------------------------------------------------------------------
      // static helper methods
      //----------------------------------------------------------------------------

      /**
      * Reads a file into a HashMap. The file should contain lines of the format
      * "key\tvalue\n"
      * @returns a hashmap of the given file
      */
      @SuppressWarnings("unchecked")
      private static HashMap ReadHashMapFromFile(String filename) throws FileNotFoundException, IOException {
      BufferedReader in = null;
      HashMap map = null;
      try {
      in = new BufferedReader(new FileReader(filename));
      String line;
      map = new HashMap();
      while ((line = in.readLine()) != null) {
      String[] fields = line.split("\\t", 2);
      if (fields.length != 2) continue; //just ignore "invalid" lines
      map.put(fields[0], fields[1]);
      }
      } finally {
      if(in!=null) in.close(); //may throw IOException
      }
      return(map); //returning a reference to local variable is safe in java (unlike C/C++)
      }

      /**
      * Process the given file
      * @returns String contains the whole file.
      */
      private static String ProcessFile(Map words, String filename) throws FileNotFoundException, IOException {
      BufferedReader in = null;
      StringBuffer out = null;
      try {
      in = new BufferedReader(new FileReader(filename));
      out = new StringBuffer();
      String line = null;
      while( (line=in.readLine()) != null ) {
      out.append(SearchAndReplaceWordsInText(words, line)+"\n");
      }
      } finally {
      if(in!=null) in.close(); //may throw IOException
      }
      return out.toString();
      }

      /**
      * Replaces all occurrences in text of each key in words with it's value.
      * @returns String
      */
      private static String SearchAndReplaceWordsInText(Map words, String text) {
      Iterator it = words.keySet().iterator();
      while( it.hasNext() ) {
      String key = (String)it.next();
      text = text.replaceAll("\\b"+key+"\\b", (String)words.get(key));
      }
      return text;
      }

      /**
      * @returns: s with the first letter capitalized
      */
      String capitalize(String s)
      {
      return s.substring(0,0).toUpperCase() + s.substring(1);
      }

      }