From 6523455b8fa9baa91920071f18c3bc22ec163ed4 Mon Sep 17 00:00:00 2001 From: tanghao Date: Fri, 31 Jul 2020 14:35:57 +0800 Subject: fix: 关闭没有处理的流 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/nis/util/YamlUtil.java | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nis/util/YamlUtil.java b/src/main/java/com/nis/util/YamlUtil.java index e598d1b..291bd3b 100644 --- a/src/main/java/com/nis/util/YamlUtil.java +++ b/src/main/java/com/nis/util/YamlUtil.java @@ -151,6 +151,9 @@ public class YamlUtil { FileInputStream fileInputStream =null; OutputStream out = null; OutputStreamWriter osw = null; + FileInputStream fileInputStream2 =null; + OutputStream out2 = null; + OutputStreamWriter osw2 = null; try { // 修改prometheus.yml配置文件 File ymlfile = new File(ymlPath); @@ -172,14 +175,14 @@ public class YamlUtil { // 文件不存在 创建文件 ruleYmlfile.createNewFile(); } - FileInputStream fileInputStream2 = new FileInputStream(ruleYmlfile); + fileInputStream2 = new FileInputStream(ruleYmlfile); Map map2 = y.loadAs(fileInputStream2, Map.class); if(map2==null) { map2=new HashMap(); } map2.put("groups", param.get("groups")); - OutputStream out2 = new FileOutputStream(ruleYmlfile); - OutputStreamWriter osw2 = new OutputStreamWriter(out2, "UTF-8"); + out2 = new FileOutputStream(ruleYmlfile); + osw2 = new OutputStreamWriter(out2, "UTF-8"); y.dump(map2,osw2); // RuntimeUtil.run(null, null, new String[]{"/bin/sh", "-c","ps -ef |grep prometheus|grep -v grep|awk \'{print $2}\'|xargs kill -hup"}); @@ -211,6 +214,27 @@ public class YamlUtil { e.printStackTrace(); } } + if(fileInputStream2!=null) { + try { + fileInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(out2!=null) { + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if(osw2!=null) { + try { + osw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } return flag; -- cgit v1.2.3