如何从文件中读取此Huffman项目?

时间:2016-11-22 22:29:07

标签: java filereader

我正在研究一个霍夫曼项目,我已经掌握了基本功能。它计算给定字符串中的字符并显示它们;但是,我希望它从文件中读取而不是手​​动给它打印一个字符串。

我的代码如下,任何关于使其打印文本文件内容的建议都非常有用。

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {

    String message = "Hello";

    // Convert the string to char array

    char[] msgChar = message.toCharArray();
    ArrayList<Character> characters = new ArrayList<Character>();

    /*
     * Get a List of all the chars which are present in the string No
     * repeating the characters!
     */
    for (int i = 0; i < msgChar.length; i++) {
        if (!(characters.contains(msgChar[i]))) {
            characters.add(msgChar[i]);
        }
    }

    System.out.println(message);
    System.out.println("");

    /* Count the number of occurrences of Characters */
    int[] countOfChar = new int[characters.size()];

    /* Fill The Array Of Counts with one as base value */
    for (int x = 0; x < countOfChar.length; x++) {
        countOfChar[x] = 0;
    }

    /* Do Actual Counting! */
    for (int i = 0; i < characters.size(); i++) {
        char checker = characters.get(i);
        for (int x = 0; x < msgChar.length; x++) {
            if (checker == msgChar[x]) {
                countOfChar[i]++;
            }
        }
    }

    /* Sort the arrays is descending order */
    for (int i = 0; i < countOfChar.length - 1; i++) {
        for (int j = 0; j < countOfChar.length - 1; j++) {
            if (countOfChar[j] < countOfChar[j + 1]) {
                int temp = countOfChar[j];
                countOfChar[j] = countOfChar[j + 1];
                countOfChar[j + 1] = temp;

                char tempChar = characters.get(j);
                characters.set(j, characters.get(j + 1));
                characters.set(j + 1, tempChar);
            }
        }
    }

    /* Print Out The Frequencies of the Characters */
    for (int x = 0; x < countOfChar.length; x++) {
        System.out.println(characters.get(x) + " - " + countOfChar[x]);
    }

}
}

2 个答案:

答案 0 :(得分:0)

String line;
try (
    InputStream fis = new FileInputStream("file_name");
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);
) {
 while ((line = br.readLine()) != null) {
    // Deal with the line
}
}

更清洁的方式是     List<String> lines = Files.readAllLines(path);

答案 1 :(得分:0)

由于我不知道您使用的是哪个版本的java,因此该方法将解决:

private static String readFromFile(String filename) {
    try {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = reader.readLine()) != null)
            sb.append(line).append("\n");
        reader.close();
        return sb.toString();
    } catch (IOException e) {
        System.err.println("File " + filename + " cannot be read: " + e.getMessage());
        return null;
    }
}

在您的代码中,您可以像这样使用它:

public static void main(String[] args) {
    String message = readString("testfile.txt");
    if(message == null)
        return; // cannot read!