读取文件并对单词进行排序

时间:2015-10-17 22:58:22

标签: java file sorting

我正在执行这部分代码:

 Map<String, Integer> wordMap = new HashMap<>();
        File file = new File("words.txt");
         Scanner scanner = new Scanner(file);
         while (scanner.hasNextLine()) {
             String word = scanner.nextLine();
             int count = 1;
             if (wordMap.containsKey(word)) {                    
                 count = wordMap.get(word).intValue();
                 count++;
                 wordMap.remove(word);
                 wordMap.put(word, count);
             } else {
                 wordMap.put(word, count);
             }
           System.out.println(wordMap);

我只想对文件&#34; words.txt&#34;中的单词进行排序。 但我得到的结果是 -

{{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1}
{{\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1}
{{\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1}
{{\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1}
{\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1}
{=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1}
{=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, \f0\fs24 \cf0 egg\=1}
{=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, \f0\fs24 \cf0 egg\=1}
{=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, \f0\fs24 \cf0 egg\=1}
{=1, moon\=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, \f0\fs24 \cf0 egg\=1}
{=1, moon\=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{=1, tooth\=1, moon\=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{=1, love\=1, tooth\=1, moon\=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{=1, love\=1, tooth\=1, moon\=1, blue\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, post\=1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, post\=1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, post\=1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, post\=1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}
{the\=1, =1, post\=1, blue\=1, {\colortbl;\red255\green255\blue255;}=1, {\fonttbl\f0\fswiss\fcharset0 Helvetica;}=1, love\=1, moon\=1, tooth\=1, please\=1, \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0=1, dog\=1, }=1, {\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf110=1, end\=1, \margl1440\margr1440\vieww28600\viewh15800\viewkind0=1, fish\=1, \f0\fs24 \cf0 egg\=1}

代码有什么问题??? 感谢

3 个答案:

答案 0 :(得分:1)

  1. 你说你想要单词,但你不是在读单词:你在读行:

     while (scanner.hasNextLine()) {
         String word = scanner.nextLine();
    
  2. 您的文件不是简单的文本文件,而是RTF文件。因此它包含RTF格式标记(字体,粗体,斜体等)

  3. 您永远不会在地图中存储除1之外的任何内容:如果“单词”已经在地图中,您只需将计数从1增加到2,并且不对计数执行任何操作。您需要从地图中获取该单词的计数,将其递增,然后将其放回地图中。

答案 1 :(得分:1)

  1. 为了按字母顺序对单词进行排序,最好使用TreeMap而不是HashMap。 TreeMaps按顺序自动排序键。
  2. 您看到的输出相当于调用HashMap.toString()。相反,试试这个:

    for (String key : wordMap.keys()) {
        System.out.println(key + ": " + wordMap.get(key));
    }
    

答案 2 :(得分:0)

问题是,我使用TextEditor创建了文档,它创建了.rtf doc。 我将它转换为.txt但它可能已经损坏了它。

我创建了不同的.txt文件并且有效!!

 Map<String, Integer> wordMap = new TreeMap<>();
            File file = new File("wordCount.txt");
             Scanner scanner = new Scanner(file);
             while (scanner.hasNextLine()) {
                 String word = scanner.nextLine();
                 int count = 1;
                 if (wordMap.containsKey(word)) {                    
                    count = wordMap.get(word).intValue();
                     count++;
                     wordMap.remove(word);
                     wordMap.put(word, count);
                 } else {
                     wordMap.put(word, count);
                 }
             }
               System.out.println(wordMap);
             scanner.close();