我試圖實現從.txt文件中提取字符的Huffman算法。 txt文件包含一段文字。現在,當我運行該程序,像這樣FileReader/Scanner讀取文件名,而不是.txt文件的內容
java HuffmanCode large.txt
它產生的輸出,但它評估的是文件的名稱,「large.txt」,而不是文件large.txt內的文本。我如何讓它讀取內容?謝謝你的幫助。
public static void main(String[] args) throws IOException {
String inputFileName = args[0];
FileReader reader = new FileReader(inputFileName);
Scanner in = new Scanner(reader);
int[] charFreqs = new int[256];
// read each character and record the frequencies
for (char c : inputFileName.toCharArray())
charFreqs[c]++;
// build tree
HuffmanTree tree = buildTree(charFreqs);
// print out results
System.out.println("SYMBOL\tWEIGHT\tHUFFMAN CODE");
printCodes(tree, new StringBuffer());
}
}
Ahem - 'inputFileName.toCharArray()'。你的IDE未使用的警告應該讓你知道。 –