如何写入来自程序的输出文件,如矩阵,Java

时间:2018-05-24 09:34:48

标签: java file matrix output

我正在尝试计算文件中的重复单词,我想将文件写入我的输出

   public static void main(String a[]){
    MaxDuplicateWordCount mdc = new MaxDuplicateWordCount();
    Map<String, Integer> wordMap = mdc.getWordCount("C:/Users/Arnoldas/Desktop/Hamletas.txt");
    List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);
    for(Map.Entry<String, Integer> entry:list){
        System.out.println(entry.getKey()+" ==== "+entry.getValue());
    }
}

输出现在看起来像:

hamletui ==== 4
šmėkla ==== 2
jo ==== 2
hamletas ==== 1
danijos ==== 1
pagrindiniam ==== 1
mirusio ==== 1
princui ==== 1
herojui, ==== 1
apsireiškusi ==== 1
karalystės ==== 1
paveda ==== 1
parketas ==== 1
herojui ==== 1
žudikui ==== 1
neseniai ==== 1
omletas ==== 1
atkeršyti ==== 1
tėvo ==== 1

我想将此输出放到文件中,它应该如下所示:

               | hamletui | šmėkla | jo  | hamletas | danijos | pagrindiniam |   ...
-------------------------------------------------------------------------------------
Hamletas.txt   |     4    |    2   |  2  |    0     |    0    |      0       |   ...
-------------------------------------------------------------------------------------
Other_file.txt |     0    |    0   |  6  |    3     |    4    |      1       |   ...

有可能这样做吗?另外我想获得输出和其他文件。  有什么想法吗?

1 个答案:

答案 0 :(得分:1)

行。如何使用表将其写为HTML-File的示例。 输入是一个带有文件名作为键的地图和带有计数的单词地图。 首先,它查看地图中提到的单词。然后它写HTML。 第一个表行包含所有这些单词,在此之后,对于每个文件名,将使用单词的计数创建一行。

public static void main(String a[]) {
    Map<String, Map<String, Integer>> wordMaps = new HashMap<>();

    wordMaps.put("Hamletas.txt", new HashMap<String, Integer>());
    wordMaps.get("Hamletas.txt").put("hamletui", 4);
    wordMaps.get("Hamletas.txt").put("šmėkla", 2);
    wordMaps.get("Hamletas.txt").put("jo", 2);

    wordMaps.put("Other_file.txt", new HashMap<String, Integer>());
    wordMaps.get("Other_file.txt").put("jo", 6);
    wordMaps.get("Other_file.txt").put("hamletas", 3);
    wordMaps.get("Other_file.txt").put("danijos", 4);
    wordMaps.get("Other_file.txt").put("pagrindiniam", 1);

    writeHtmlFile("example.html", wordMaps);
}

public static void writeHtmlFile(String filename, Map<String, Map<String, Integer>> wordMaps) {
    // get all words that are duplicates
    Set<String> words = new HashSet<>();
    for (Map<String, Integer> map : wordMaps.values()) {
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                words.add(entry.getKey());
            }
        }
    }

    try (PrintWriter out = new PrintWriter(new FileWriter(filename))) {
        out.println("<!DOCTYPE html><html>");
        out.println("<head><meta charset=\"utf-8\"/></head");
        out.println("<body>");
        out.println("<table border=\"1\">");

        // write Headline
        out.println("<tr>");
        out.println("<td>&#160;</td>"); // empty cell
        for (String word : words) {
            out.println("<td>" + word + "</td>"); // empty cell
        }
        out.println("</tr>");

        // write for all files
        for (Map.Entry<String, Map<String, Integer>> fileWordMap : wordMaps.entrySet()) {
            out.println("<tr>");
            out.println("<td>" + fileWordMap.getKey() + "</td>"); // filename
            Map<String, Integer> wordMap = fileWordMap.getValue();
            for (String word : words) {
                out.println("<td align=\"right\">" + (wordMap.containsKey(word) ? wordMap.get(word) : 0) + "</td>"); // number per
                                                                                                     // word or 0
            }
            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
    } catch (IOException e) {

    }
}