文件内多行字符串 转 字符串集合 & 字符串集合写入文档并指定 文档编码

1.文档转集合

public class FileUtil {

    public static void txt2String(File file, List list) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
                list.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.集合写入文档

@Override
    public void saveKeywordFile() {
        List<EsKeyword> esKeywords = list((new QueryWrapper<EsKeyword>())
            .select("keyword_desc")
                .orderByAsc("update_date")
        );
        List<String> strings = esKeywords.stream().distinct()
                .map(esKeyword -> esKeyword.getKeywordDesc()).collect(Collectors.toList());
        String str = CollectionUtil.join(strings, "\n");

        try {
            File file = new File(keywordFilePath);
            OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            oStreamWriter.write(str);
            oStreamWriter.close();
            logger.info("保存文件" + keywordFilePath + "成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

 

posted @ 2020-05-22 15:22  官萧何  阅读(235)  评论(0)    收藏  举报