reader.java

来自「基于哈夫曼树的压缩解压程序源代码」· Java 代码 · 共 59 行

JAVA
59
字号
package logical;

import java.io.*;

//Read the file and get the huffman tree
public class Reader {
	private long[] weight = new long[256];

	// Cause the numbers are between 0 and 255, we use an long array "weight" to
	// store the weight of the node
	private String textName;// Get the name of the file

	private Node[] tree;
	private String[] codes;

	Reader(String textName) throws IOException {
		this.textName = textName;
		// Get the content of the file
		BufferedInputStream input = new BufferedInputStream(
				new FileInputStream(textName));
		int temp;
		while ((temp = input.read()) != -1) {
			weight[temp]++;// Count the weight of all the node
		}
		input.close();
		// According to the weights of all the node to
		// construct the huffman tree
		HuffmanTree huff = new HuffmanTree(weight);

		codes = huff.getCode();
		tree = huff.getTree();
	}

	// Set the name of the file which needs to be read
	public void setTextName(String textName) {
		this.textName = textName;
	}

	// Get the name of the file which needs to be read
	public String getTextName() {
		return textName;
	}

	// Get the weight of all bytes which are between 0 and 255
	public long[] getweight() {
		return weight;
	}

	// Get the huffmantree 
	public Node[] getHuffmanTree() {
		return tree;
	}
	// Get the huffmancode
	public String[] getHuffmanCode() {
		return codes;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?