Posted By

jarlah on 08/30/13


Tagged

indexer


Versions (?)

Scanner and Tokenizer for file indexing


 / Published in: Java
 

Use Scanner instead of BufferedReader and StringTokenizer for parsing the line. I see a potential for using Scanner for both use cases. But it was a major improvement to get rid of the split arrays. In addition the BufferedReader was not closed. The scanner is in the final loop. Also, use of static is highly discouraged. Object oriented approach is better and no reason to expose map or list, or the parse method. Both is now private. String expectations about file encoding. Lets assume we only handle UTF-8 files. No fishy norwegian characters.

  1. package com.java.test;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9. import java.util.StringTokenizer;
  10.  
  11. /***
  12.  * input file format:
  13.  *
  14.  * foo|bar
  15.  * foo2|bar2
  16.  * .
  17.  * .
  18.  */
  19. public class Indexer {
  20. private Map<String, String> indexerMap = new HashMap<String, String>();
  21.  
  22. public Indexer(String filename) throws IOException{
  23. indexerMap = parseFileIntoMap(filename);
  24. }
  25.  
  26. private Map<String, String> parseFileIntoMap(String filename) throws IOException {
  27. Map<String, String> map = new HashMap<String, String>();
  28.  
  29. Scanner scanner = null;
  30. try {
  31. scanner = new Scanner(new File(filename), "UTF-8");
  32. scanner.useDelimiter("[\r\n]+");
  33. while (scanner.hasNext())
  34. {
  35. StringTokenizer tokenizer = new StringTokenizer(scanner.next(), "\\|");
  36. if (tokenizer.countTokens() == 2) {
  37. String key = tokenizer.nextElement().toString();
  38. String value = tokenizer.nextElement().toString();
  39. map.put(key, value);
  40. }
  41. }
  42. } catch (FileNotFoundException e) {
  43. throw new IllegalArgumentException("Could not load file indexing! The file "+filename+" does not exist.", e);
  44. }finally {
  45. if(scanner != null)
  46. scanner.close();
  47. }
  48.  
  49. return map;
  50. }
  51.  
  52. /***
  53. * Returns the value from the map if key is not null
  54. */
  55. public String getValue(String key) {
  56. if (key == null) {
  57. return null;
  58. } else {
  59. return indexerMap.get(key);
  60. }
  61. }
  62. }

Report this snippet  

You need to login to post a comment.