zoukankan      html  css  js  c++  java
  • 文件内多行字符串 转 字符串集合 & 字符串集合写入文档并指定 文档编码

    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, "
    ");
    
            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();
            }
    
        }
  • 相关阅读:
    MyBatis与Spring的整合
    Spring核心AOP(面向切面编程)
    Spring核心IoC(控制反转)
    动态SQL
    SQL映射文件
    初识MyBatis
    注解和反射
    Linux配置SVN和MemCached
    Java Web Day10
    Java Web Day9
  • 原文地址:https://www.cnblogs.com/guanxiaohe/p/12937374.html
Copyright © 2011-2022 走看看