每行一个单词,对其进行分类输出为Markdown的无序列表的形式

我们可能存在这样的一个文件:

apple
define
window
run

现在想对其分类, 并以Markdown无序列表的形式输出, 如: 常见单词缩写.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class AbbreviationConverter {

    public static void main(String[] args) throws Exception {
        try {
            InputStream in = new FileInputStream(new File("src/abbreviation-src.txt"));
            OutputStream out = new FileOutputStream(new File("src/abbreviation-dest.txt"));
            convert(in, out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void convert(InputStream in, OutputStream out) throws Exception, Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        String line = null;
        List<String> abbrList = new ArrayList<String>();
        Map<String, List<String>> abbrMap = new HashMap<>();
        try {
            while ((line = br.readLine()) != null) {
                if (line.length() > 0) {
                    abbrList.add(line);
                }
            }

            // sort by ascii
            // if don't sort, the src.txt's order will change the final result
            abbrList.sort(new Comparator<String>() {

                @Override
                public int compare(String o1, String o2) {
                    return o1.compareToIgnoreCase(o2);
                }
            });

            String currLine = null;
            String nextLine = null;
            char currFirstChar = ' ';
            char nextFirstChar = ' ';
            int i = -1;
            int listLen = abbrList.size();
            List<String> tmpList = new ArrayList<>();

            while (++i < listLen) {
                currLine = abbrList.get(i);
                currFirstChar = currLine.charAt(0);
                try {
                    nextLine = abbrList.get(i + 1);
                    nextFirstChar = nextLine.charAt(0);
                    if (nextFirstChar == currFirstChar) {
                        tmpList.add(currLine);
                    } else {
                        tmpList.add(currLine);
                        abbrMap.put((char) (currFirstChar - 32) + "", tmpList);

                        // 引用类型会清空之前的数据,也就是会得到 A -> []
                        // tmpList.clear();
                        tmpList = new ArrayList<>();

                    }
                } catch (Exception e) {
                    // now currLine = abbrList.get(listLen - 1);
                    //
                    // if (currFirstChar == abbrList.get(listLen - 2).charAt(0))
                    // {
                    // tmpList.add(currLine);
                    // abbrMap.put((char)(currFirstChar - 32) + "", tmpList);
                    // } else {
                    // tmpList = new ArrayList<>();
                    //
                    // tmpList.add(currLine);
                    // abbrMap.put((char)(currFirstChar - 32) + "", tmpList);
                    // }

                    if (currFirstChar != abbrList.get(listLen - 2).charAt(0)) {
                        tmpList = new ArrayList<>();
                    }
                    tmpList.add(currLine);
                    abbrMap.put((char) (currFirstChar - 32) + "", tmpList);

                }
            }

            Set<String> keys = abbrMap.keySet();
            for (String key : keys) {
                List<String> tmpResultList = abbrMap.get(key);
                bw.write("- " + key + "\r\n");
                for (String tmpResult : tmpResultList) {
                    bw.write("  " + tmpResult + "\r\n");
                }
                bw.write("\r\n");
            }
            br.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值