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(); } }