diff --git a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java index c118a68b66f..ac66a1cd49a 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java @@ -42,20 +42,10 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti * @since IO 2.1 */ public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException { - OutputStream out = null; - try { - out = openOutputStream(file, append); + try (OutputStream out = openOutputStream(file, append)) { out.write(data); - out.close(); // don't swallow close Exception if copy completes normally - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } /** @@ -111,10 +101,8 @@ private static boolean isAuthCommand(String command) { * @param file the file to save the history */ public static void saveCommandHistory(List history, File file) { - OutputStream out = null; - try { - out = new BufferedOutputStream(openOutputStream(file, false)); - for (int[] command: history) { + try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) { + for (int[] command : history) { String commandStr = Helper.fromCodePoints(command); if (isAuthCommand(commandStr)) { command = AUTH_CODEPOINTS; @@ -127,15 +115,8 @@ public static void saveCommandHistory(List history, File file) { } } catch (IOException e) { // ignore - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } public static List loadCommandHistory(File file) { @@ -167,10 +148,8 @@ public static List loadCommandHistory(File file) { * @param file the file to save the history */ public static void saveCommandHistoryString(List history, File file) { - OutputStream out = null; - try { - out = new BufferedOutputStream(openOutputStream(file, false)); - for (String command: history) { + try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) { + for (String command : history) { if (!StringUtils.isBlank(command)) { if (isAuthCommand(command)) { command = ArthasConstants.AUTH; @@ -181,15 +160,8 @@ public static void saveCommandHistoryString(List history, File file) { } } catch (IOException e) { // ignore - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } public static List loadCommandHistoryString(File file) { @@ -218,8 +190,7 @@ public static List loadCommandHistoryString(File file) { } public static String readFileToString(File file, Charset encoding) throws IOException { - FileInputStream stream = new FileInputStream(file); - try { + try (FileInputStream stream = new FileInputStream(file)) { Reader reader = new BufferedReader(new InputStreamReader(stream, encoding)); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; @@ -228,8 +199,6 @@ public static String readFileToString(File file, Charset encoding) throws IOExce builder.append(buffer, 0, read); } return builder.toString(); - } finally { - stream.close(); } }