Java输出文件到web项目的classes下

需求:从数据库加载最新的疾病名称数据写到classes目录下的lucene扩展词典

    public HashMap<String, String> updateAllIndex() {
        HashMap<String, String> resultHashMap = Maps.newHashMap();
        FyDiseaseLuceneDao diseaseLuceneDao = new FyDiseaseLuceneDao();
        List<FyDisease> diseaseList = fyDiseaseDao.findListByIstatus1(); //获取全部 状态 为1 的 数据
        if (diseaseList != null && diseaseList.size() > 0) {
            //更新疾病扩展词表 
            //System.err.println(this.getClass().getClassLoader().getResourceAsStream(""));
            //try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("disease.dic")))) {
            String pathStr = null;
            if (System.getProperty("os.name").toLowerCase().contains("windows"))
                pathStr = this.getClass().getClassLoader().getResource("").getPath().substring(1);
            else
                pathStr = this.getClass().getClassLoader().getResource("").getPath();

            try (BufferedWriter out = new BufferedWriter(new FileWriter(pathStr+"disease.dic"))) {
                diseaseList.forEach(d -> {
                    try {
                        out.write(d.getIndexName());
                        out.newLine();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 

        // 存lucene 索引
             try {
                diseaseLuceneDao.addIndexBatch(diseaseList, b -> {
                    Document doc = new Document();
                    doc.add(new StringField("id", b.getId(), Store.YES));
                    doc.add(new StringField("indexName", b.getIndexName(), Store.YES));
                    return doc;
                });
                resultHashMap.put("code", "200");
                resultHashMap.put("msg", "更新lucene索引成功");
            } catch (Exception e) {
                resultHashMap.put("code", "500");
                resultHashMap.put("msg", "更新lucene索引异常");
                e.printStackTrace();
            }
        }

         return resultHashMap;

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值