Advertisement
Guest User

FileReader

a guest
Mar 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.HashMap;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5.  
  6. public class FileReader {
  7. private File f;
  8.  
  9. public FileReader(){
  10. f = new File("large_text.txt");
  11. }
  12. public void read(Heap<HuffmanNode> h) throws FileNotFoundException {
  13. Scanner scn = new Scanner(f);
  14. HashMap<Character, Integer> freqTable = new HashMap<>();
  15. while(scn.hasNextLine()) {
  16. String s = scn.nextLine() + "\n"; //get next line of text
  17. for(int i = 0; i < s.length(); i++) { //parse each char of the line
  18. Character c = s.charAt(i);
  19. if(freqTable.containsKey(c)) { //if it's not a new character
  20. freqTable.replace(c, freqTable.get(c) + 1); //++ the current entry
  21. }
  22. else {
  23. freqTable.put(c, Integer.valueOf(1)); //if it doesn't exist yet put it in with value 1
  24. }
  25. }
  26. }
  27. //System.out.println(freqTable); //DEBUG
  28. scn.close(); //done with scanner
  29. for(Character c : freqTable.keySet()) { //create new nodes and put them into the heap
  30. h.add(new HuffmanNode(c.toString(), freqTable.get(c)));
  31. }
  32. System.out.println(h);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement