public static void main(String[] args) { byte[] b = "ABCDEFGHIJK".getBytes(); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("./out.txt"); fileOutputStream.write(b); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null){ try { fileOutputStream.close(); } catch (IOException ignored) { //DO NOT CARE... } } } }
We do not have to close the resource, Java will handle it for us.
public static void main(String[] args) { byte[] b = "ABCDEFGHIJK".getBytes(); try (FileOutputStream fileOutputStream = new FileOutputStream("./out.txt")) { fileOutputStream.write(b); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }