Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.HashMap;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class FileReader {
- private File f;
- public FileReader(){
- f = new File("large_text.txt");
- }
- public void read(Heap<HuffmanNode> h) throws FileNotFoundException {
- Scanner scn = new Scanner(f);
- HashMap<Character, Integer> freqTable = new HashMap<>();
- while(scn.hasNextLine()) {
- String s = scn.nextLine() + "\n"; //get next line of text
- for(int i = 0; i < s.length(); i++) { //parse each char of the line
- Character c = s.charAt(i);
- if(freqTable.containsKey(c)) { //if it's not a new character
- freqTable.replace(c, freqTable.get(c) + 1); //++ the current entry
- }
- else {
- freqTable.put(c, Integer.valueOf(1)); //if it doesn't exist yet put it in with value 1
- }
- }
- }
- //System.out.println(freqTable); //DEBUG
- scn.close(); //done with scanner
- for(Character c : freqTable.keySet()) { //create new nodes and put them into the heap
- h.add(new HuffmanNode(c.toString(), freqTable.get(c)));
- }
- System.out.println(h);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement