Scanner and Tokenizer for file indexing


/ Published in: Java
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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("[
  33. ]+");
  34. while (scanner.hasNext())
  35. {
  36. StringTokenizer tokenizer = new StringTokenizer(scanner.next(), "\\|");
  37. if (tokenizer.countTokens() == 2) {
  38. String key = tokenizer.nextElement().toString();
  39. String value = tokenizer.nextElement().toString();
  40. map.put(key, value);
  41. }
  42. }
  43. } catch (FileNotFoundException e) {
  44. throw new IllegalArgumentException("Could not load file indexing! The file "+filename+" does not exist.", e);
  45. }finally {
  46. if(scanner != null)
  47. scanner.close();
  48. }
  49.  
  50. return map;
  51. }
  52.  
  53. /***
  54. * Returns the value from the map if key is not null
  55. */
  56. public String getValue(String key) {
  57. if (key == null) {
  58. return null;
  59. } else {
  60. return indexerMap.get(key);
  61. }
  62. }
  63. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.